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.push_back( t );return ;}
10         for(int i=0; i<sett[n].size(); i++)    DFS(sett, siz+1, t+sett[n][i] );
11     }
12
13     vector<string> letterCombinations(string digits) {
14         if(digits.empty())  return vector<string> ();
15         str=digits;
16         const string mapp[]={"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
17         DFS(mapp, 0, "");
18         return ans;
19     }
20 };

AC代码

时间: 2024-10-23 16:47:23

LeetCode 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

LeetCode: Letter Combinations of a Phone Number [018]

[题目] 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:Letter Combinations of a Phone Number 手机键盘字母映射

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

LeetCode: Letter Combinations of a Phone Number 解题报告

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

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

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

[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

[LeetCode] Letter Combinations of a Phone Number(bfs)

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