Leetcode17--->Letter Combinations of a Phone Number(电话号码对应的字母的组合)

题目:

给定一个数字字符串,返回数字所能代表的所有字母组合;

举例:

Input:Digit string "23"

Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

解题思路:

1. 首先要判断给定的数字字符串是否合法(isDigit()方法),即是否只包含0~9的数字,如果还包含其他,则直接返回空;

2. 其次要将每个电话号码所对应的字符串保存起来,这里我使用的是HashMap,key是数字,value是数字所对应的字符串;

3. 采用递归的方式将数字所代表的字母连接起来;举例:234,则对应的是abc,def,ghi,此时首先选择a,然后选择d,然后选择g,得到一个结果adg,选择h,得到一个结果adh,选择i,得到结果adi;选择e.选择g得到结果aeg,以此类推......

代码如下:

 1 public class Solution {
 2     public List<String> letterCombinations(String digits) {
 3         List<String> res = new ArrayList<String>();
 4         if(digits == null || digits.length() < 1){
 5             return res;
 6         }
 7         if(!isDigit(digits)) return res;
 8         HashMap<Character, String> hm = new HashMap<Character, String>();
 9         hm.put(‘0‘, "");
10         hm.put(‘1‘, "");
11         hm.put(‘7‘, "pqrs");
12         hm.put(‘8‘, "tuv");
13         hm.put(‘9‘, "wxyz");
14         String str = "abcdefghijklmno";
15         int i = 2;;
16         int j = 0;
17         while(i < 7){
18             hm.put((char)(i + ‘0‘), str.substring(j, j + 3));
19             j = j + 3;
20             i++;
21         }
22         char[] ch = new char[digits.length()];
23         isCal(digits, 0, ch, res, hm);
24         return res;
25     }
26     // 判断给定的数字字符串是否合法,不合法,统一返回空
27     public boolean isDigit(String digits){
28         for(int i = 0; i < digits.length(); i++){
29             if(digits.charAt(i) < ‘0‘ || digits.charAt(i) >‘9‘)
30                 return false;
31         }
32         return true;
33     }    // index代表的是第几个数字
34     public void isCal(String digits, int index, char[] ch,  List<String> res, HashMap<Character, String> hm){
35         if(index == digits.length()){  // 如果成立,表明已经连接到最后一个数字了,因此要将结果加入res
36             res.add(new String(ch));
37             return;
38         }
39         char c = digits.charAt(index);
40         for(int i = 0; i < hm.get(c).length(); i++){  //
41             ch[index] = hm.get(c).charAt(i);  // 获取index所对应数字的字符串中的字符
42             isCal(digits, index + 1, ch, res, hm); // 获取下一个数字所对应的字符串中的字符
43         }
44
45     }
46 }


时间: 2024-10-26 16:34:04

Leetcode17--->Letter Combinations of a Phone Number(电话号码对应的字母的组合)的相关文章

[LeetCode] 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

LeetCode17 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"

lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] 注意 以上的答案是按照词

[LintCode] 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. Notice Although the above answer is in lexicographical order, your answe

[Java]LeetCode17 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

19.1.26 [LeetCode17] Letter Combinations of a Phone Number

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 below. Note that 1 does not map to any letters.

24.Letter Combinations of a Phone Number(电话号对应的字符组合)

Level: ??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 below. Note that 1 does no

LeetCode Letter Combinations of a Phone Number 电话号码组合

题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. 1 class Solution { 2 public: 3 vector<string> ans; 4 string str; 5 6 void DFS(const string sett[], int siz, string t ) 7 { 8 int n=str[siz]-'0'; 9 if(siz==str.size()){ans.pu

Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:["ad", "ae", "af", "bd", "be", "bf&qu