|
| 1 | +# 17. 电话号码的字母组合 |
| 2 | + |
| 3 | + |
| 4 | +[url](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/) |
| 5 | + |
| 6 | +## 题目 |
| 7 | + |
| 8 | +给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。 |
| 9 | + |
| 10 | +给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +``` |
| 16 | +输入:digits = "23" |
| 17 | +输出:["ad","ae","af","bd","be","bf","cd","ce","cf"] |
| 18 | +输入:digits = "" |
| 19 | +输出:[] |
| 20 | +输入:digits = "2" |
| 21 | +输出:["a","b","c"] |
| 22 | +``` |
| 23 | + |
| 24 | +## 方法 |
| 25 | + |
| 26 | + |
| 27 | +## code |
| 28 | + |
| 29 | +### js |
| 30 | + |
| 31 | +```js |
| 32 | +let letterCombinations = digits => { |
| 33 | + let dfs = (digits, sb) => { |
| 34 | + if (sb.length === digits.length) { |
| 35 | + ret.push(sb); |
| 36 | + return; |
| 37 | + } |
| 38 | + let c = digits[sb.length] - '0'; |
| 39 | + let cur = KEYS[c]; |
| 40 | + for (const a of cur) { |
| 41 | + sb += a; |
| 42 | + dfs(digits, sb); |
| 43 | + sb = sb.substring(0, sb.length - 1); |
| 44 | + } |
| 45 | + }; |
| 46 | + let KEYS = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]; |
| 47 | + let ret = []; |
| 48 | + if (digits === null || digits.length === 0) |
| 49 | + return ret; |
| 50 | + dfs(digits, ""); |
| 51 | + return ret; |
| 52 | +}; |
| 53 | +``` |
| 54 | + |
| 55 | +### go |
| 56 | + |
| 57 | +```go |
| 58 | +var KEYS = []string{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"} |
| 59 | +var res1 []string |
| 60 | +func letterCombinations(digits string) []string { |
| 61 | + if len(digits) == 0 { |
| 62 | + return res1 |
| 63 | + } |
| 64 | + dfs(digits, "") |
| 65 | + return res1 |
| 66 | +} |
| 67 | +func dfs(digits string, sb string) { |
| 68 | + if len(sb) == len(digits) { |
| 69 | + res1 = append(res1, sb) |
| 70 | + return |
| 71 | + } |
| 72 | + c := digits[len(sb)] - byte('0') |
| 73 | + cur := KEYS[c] |
| 74 | + for _, v := range cur { |
| 75 | + sb += string(v) |
| 76 | + dfs(digits, sb) |
| 77 | + sb = sb[0:len(sb) - 1] |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### java |
| 83 | + |
| 84 | +```java |
| 85 | +class Solution { |
| 86 | + private static final String[] KEYS = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; |
| 87 | + List<String> ret = new ArrayList<>(); |
| 88 | + public List<String> letterCombinations(String digits) { |
| 89 | + if(digits == null || digits.length() == 0) return ret; |
| 90 | + dfs(digits, new StringBuilder()); |
| 91 | + return ret; |
| 92 | + } |
| 93 | + |
| 94 | + private void dfs(String digits, StringBuilder sb) { |
| 95 | + if (sb.length() == digits.length()){ |
| 96 | + ret.add(sb.toString()); |
| 97 | + return; |
| 98 | + } |
| 99 | + int c = digits.charAt(sb.length()) - '0'; |
| 100 | + String cur = KEYS[c]; |
| 101 | + for (char a : cur.toCharArray()){ |
| 102 | + sb.append(a); |
| 103 | + dfs(digits, sb); |
| 104 | + sb.deleteCharAt(sb.length() - 1); |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
0 commit comments