LeetCode 17: Letters Combination of a Phone Number

class Solution {
    private final String[] map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if (digits.length() == 0) {
            return result;
        }
        generateComb(digits.toCharArray(), result, new StringBuilder(), 0);
        return result;
    }

    private void generateComb(char[] digits, List<String> result, StringBuilder current, int index) {
        if (index == digits.length) {
            result.add(current.toString());
            return;
        }

        int pos = (int)(digits[index] - ‘0‘);

        for (int i = 0; i < map[pos].length(); i++) {
            current.append(map[pos].charAt(i));
            generateComb(digits, result, current, index + 1);
            current.setLength(current.length() - 1);
        }
    }
}
时间: 2024-10-23 08:27:10

LeetCode 17: Letters Combination of a Phone Number的相关文章

leetCode 17. Letter Combinations of a Phone Number 字符串 | 回溯 | Medium

17. Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23"Ou

Leetcode 17. Letter Combinations of a Phone Number(水)

17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given

LeetCode – Refresh – Letter Combination of a Phone Number

This is just a combination. Use hashtable to hold the number ==> chars notes: 1. check corner case : input is empty, do not return vector contains empty string. 2. mapping is char to string. Do not use index to string mapping 1 class Solution { 2 pri

Leetcode 17 Letter Combinations of a Phone Number - DFS思想

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

LeetCode #17 Letter Combinations of a Phone Number (M)

[Problem] Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae

LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)

翻译 给定一个数字字符串,返回所有这些数字可以表示的字母组合. 一个数字到字母的映射(就像电话按钮)如下图所示. 输入:数字字符串"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] 备注:尽管以上答案是无序的,如果你想的话你的答案可以是

LeetCode 17 Letter Combinations of a Phone Number 数字转为字母组合

题目: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

LeetCode 17 Letter Combinations of a Phone Number(C,C++,Java,Python)

Problem: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae&

Leetcode 17.——Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q