标签:fun script nat cond length object var == abc
给出两个数字,看它在手机的键盘上对应多种做字母组合。这用到回溯法。
var dict = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];// 0与1都没有字母
function letterCombinations(digits) { //23
var result = [], condidate = []
if (!Object(digits).length) {
return result;
}
function backtrack(start) {
if (start == digits.length) { //到最后一个数字了
result.push(condidate.join(''));
} else {
var str = dict[digits[start]];//2,3对应的字母
for (var i = 0; i < str.length; i++) {
condidate.push(str[i]);
backtrack(start + 1);
condidate.pop();
}
}
}
backtrack(0);
return result;
}
leetcode 17. Letter Combinations of a Phone Number
标签:fun script nat cond length object var == abc
原文地址:https://www.cnblogs.com/rubylouvre/p/12041636.html