当前位置:网站首页>Letter combination of LeetCode2 phone number
Letter combination of LeetCode2 phone number
2022-08-02 15:21:00 【N.S.N】
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合.
给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母.
示例:
输入:“23”
输出:[“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序.
var letterCombinations = function(digits) {
let tel = ['', 1, 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
let nums = digits.split('')
let code = nums.map(num => {
if (tel[num]) {
return tel[num]
}
})
if (digits.length === 1) {
if (parseInt(digits) > 1) {
return code[0].split('')
} else {
return []
}
} else {
let combCode = (arr) => {
let temp = []
if(arr.length > 1){
for (var i = 0; i < arr[0].length; i++) {
for (var j = 0; j < arr[1].length; j++) {
temp.push(`${
arr[0][i]}${
arr[1][j]}`)
}
}
}
arr.splice(0, 2, temp)
if (arr.length > 1) {
combCode(arr)
} else {
return temp
}
return arr[0]
}
return combCode(code)
}
};
边栏推荐
猜你喜欢
随机推荐
神经网络的设计过程
机器学习和深度学习中的梯度下降及其类型
Tensorflow常用函数
2020-02-06-快速搭建个人博客
2.4G无线小模块CI24R1超低成本
FP7195转模拟调光技术解决智能家居调光频闪和电感噪音的原理
内存申请(malloc)和释放(free)之上篇
PyTorch①---加载数据、tensorboard的使用
DP4301无线收发SUB-1G芯片兼容CC1101智能家居
基于51单片机和物联网的智能家居系统(ESP8266物联网模块)
LLVM系列第六章:函数返回值Return
让深度学习歇一会吧
TypeScript 快速进阶
kotlin Android序列化
ARMv8虚拟化
Win11系统找不到dll文件怎么修复
【我的电赛日记(一)】HMI USART串口屏
7. How to add the Click to RecyclerView and LongClick events
投资组合理论的简单介绍
设备驱动框架简介









