当前位置:网站首页>PHP算法之电话号码的字母组合
PHP算法之电话号码的字母组合
2022-08-01 22:08:00 【phpstory】

array_shift() 函数用于删除数组中的第一个元素,并返回被删除的元素。
<?php
function letterCombinations($digits) {
if (empty($digits)) {
return [];
}
if(!is_string($digits)){
$digits = (string)$digits;
}
// 先创建字典
$model = ["0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"];
// 创建一个用于模拟队列的数组
$resultList = [''];
// 循环输入$digits
for ($i=0; $i < strlen($digits); $i++) {
// 先获取当前$i对应的输入的字符串
// 再使用intval 转成整形,用于根据键值对取对应的字符串
$mappIndex = intval($digits[$i]);
// 总是判断当前$resultList的第一个元素的字符长度是否等于当前$i
while (strlen($resultList[0]) == $i) {
// 将数组$resultList开头的单元移出数组
$head = array_shift($resultList);
$str = $model[$mappIndex];
for ($j=0; $j < strlen($str); $j++) {
$resultList[] = $head.$str[$j];
}
}
}
return $resultList;
}
var_dump(letterCombinations(24));
?>
边栏推荐
猜你喜欢
随机推荐
render-props and higher order components
Getting Started Database Days4
1. @Component注解的原理剖析
找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!
LeetCode952三部曲之二:小幅度优化(137ms -> 122ms,超39% -> 超51%)
ModuleNotFoundError: No module named 'yaml'
小程序毕设作品之微信体育馆预约小程序毕业设计成品(2)小程序功能
2022 版 MySQL 巅峰教程,收藏好,慢慢看
Postman 批量测试接口详细教程
Graph Theory - Strongly Connected Component Condensation + Topological Sort
03、GO语言变量定义、函数
【牛客刷题-SQL大厂面试真题】NO4.出行场景(某滴打车)
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
dvwa 通关记录1 - 暴力破解 Brute Force
移动端人脸风格化技术的应用
VGUgarbage collector(垃圾回收器)的实现原理
kubernetes CoreDNS全解析
【C语言】猜数字小游戏
使用Jenkins做持续集成,这个知识点必须要掌握
C语言必杀技3行代码把运行速度提升4倍


![[深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道](/img/6b/d4ff120493e878fcf5c9aa728eced7.png)






