栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

算法问题:字母组合

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

算法问题:字母组合

您要保留数字->字母分配的一般结构是一个或多个数组,类似于:

// 0 = N, 1 = L, 2 = T, 3 = D, 4 = R, 5 = V or F, 6 = B or P, 7 = Z, // 8 = H or CH or J, 9 = G$numberMap = new Array (    0 => new Array("N"),    1 => new Array("L"),    2 => new Array("T"),    3 => new Array("D"),    4 => new Array("R"),    5 => new Array("V", "F"),    6 => new Array("B", "P"),    7 => new Array("Z"),    8 => new Array("H", "CH", "J"),    9 => new Array("G"),);

然后,一些递归逻辑为我们提供了类似于以下功能:

function GetEncoding($number) {    $ret = new Array();    for ($i = 0; $i < strlen($number); $i++) {        // We're just translating here, nothing special.        // $var + 0 is a cheap way of forcing a variable to be numeric        $ret[] = $numberMap[$number[$i]+0];    }}function PrintEncoding($enc, $string = "") {    // If we're at the end of the line, then print!    if (count($enc) === 0) {        print $string."n";        return;    }    // Otherwise, soldier on through the possible values.    // Grab the next 'letter' and cycle through the possibilities for it.    foreach ($enc[0] as $letter) {        // And call this function again with it!        PrintEncoding(array_slice($enc, 1), $string.$letter);    }}

递归三声欢呼!这将通过以下方式使用:

PrintEncoding(GetEncoding("052384"));

并且,如果您真的希望将其作为数组,请使用输出缓冲并使用“ n”作为拆分字符串进行爆炸。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372161.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号