当前位置:网站首页>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));
?>
边栏推荐
猜你喜欢
随机推荐
Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen
【移动Web】移动端适配
ModuleNotFoundError: No module named 'yaml'
企业公众号文章写作方向:如何写出读者认可的优质内容
Kubernetes第零篇:认识kubernetes
xctf攻防世界 Web高手进阶区 web2
The thing about npm
Today's sleep quality record 74 points
基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
程序员必备的 “ 摸鱼神器 ” 来了 !
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report
【C语言实现】求两个整数的较大值
Postman 批量测试接口详细教程
familiar friend
如何防范 DAO 中的治理攻击?
易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
KMP 字符串匹配问题
选择合适的 DevOps 工具,从理解 DevOps 开始
高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
得物客服热线的演进之路








