Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

电话号码的字母组合 #1059

Open
pwstrick opened this issue Jul 31, 2020 · 0 comments
Open

电话号码的字母组合 #1059

pwstrick opened this issue Jul 31, 2020 · 0 comments
Labels
Leetcode Leetcode的题目

Comments

@pwstrick
Copy link
Owner

17. 电话号码的字母组合

/**
 * @param {string} digits
 * @return {string[]}
 */
const chars = [
        ["a", "b", "c"],
        ["d", "e", "f"],
        ["g", "h", "i"],
        ["j", "k", "l"],
        ["m", "n", "o"],
        ["p", "q", "r", "s"],
        ["t", "u", "v"],
        ["w", "x", "y", "z"]
];
let results;
var letterCombinations = function(digits) {
    if(digits.length == 0)
        return [];
    results = [];
    digits = digits.split("").map(value => parseInt(value) - 2).filter(value => value >= 0);
    let route = [];
    backtrack(route, digits);
    return results;
};

function backtrack(route, digits) {
    if(digits.length == 0) {
        results.push(route.join(""));
        return;
    }
    let digit = digits.shift();
    for(let char of chars[digit]) {
        route.push(char);
        backtrack(route, [...digits]);  //复制一份数组 以免原始数组被修改
        route.pop();
    }
}
@pwstrick pwstrick added the Leetcode Leetcode的题目 label Jul 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Leetcode Leetcode的题目
Projects
None yet
Development

No branches or pull requests

1 participant