【题目】
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", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
【分析】
无
【代码】
/*********************************
* 日期:2015-01-22
* 作者:SJF0115
* 题目: 17.Letter Combinations of a Phone Number
* 网址:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
* 结果:AC
* 来源:LeetCode
* 博客:
**********************************/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> vec;
if(digits.length() <= 0){
vec.push_back(digits);
return vec;
}//if
vector<char> number;
DFS(vec,digits,number);
return vec;
}
private:
void DFS(vector<string> &vec,string digits,vector<char> &number){
string letters[] = {" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
// 一种组合完成
int curLen = number.size();
if(curLen == digits.length()){
string letter;
for(int i = 0;i < curLen;++i){
letter += number[i];
}//for
vec.push_back(letter);
return;
}//if
// 数字
int num = digits[curLen] - '0';
// 数字所对应的字母长度
int len = letters[num].length();
for(int i = 0;i < len;++i){
number.push_back(letters[num][i]);
DFS(vec,digits,number);
number.pop_back();
}//for
}
};
int main(){
Solution solution;
string number = "2";
vector<string> result = solution.letterCombinations(number);
// 输出
for(int i = 0;i < result.size();++i){
cout<<result[i]<<endl;
}//for
return 0;
}
【代码二】
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> result;
DFS(digits,0,"",result);
return result;
}
private:
void DFS(string digits,int cur,string path,vector<string> &result){
string keyboard[] = {" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
if(cur == digits.length()){
result.push_back(path);
return;
}//if
int len = keyboard[digits[cur] - '0'].length();
for(int i = 0;i < len;++i){
char c = keyboard[digits[cur] - '0'][i];
DFS(digits,cur + 1,path + c,result);
}//for
}
};
注意一种特殊情况:
分享到:
相关推荐
17. Letter Combinations of a Phone Number** https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 题目描述 Given a string containing digits from 2-9 inclusive, return all possible ...
17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap ...
第 338 章力码 LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating ...of ...Number ...17.Letter Combinations of a Phone Number 18.4Sum 19.Remove Nth Node From End
leetcode 分类LeetCode-Java-接受 这是 Leetcode 问题的 Java 解决方案。 细节 标题和答案格式 /* * 17. Letter Combinations of a Phone Number * Target: Given a string containing digits from 2-9 inclusive, ...
17.Letter Combinations of a Phone Number backtracking int solution[MAX_DIMENSION]; void Backtracking(int dimension) { if(solution is well-generated) { process solution return; } for( x = each value of...
leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag 0017 Letter Combinations of a Phone Number Medium 回溯、暴力 0034...
17.Letter Combinations of a Phone Number: 这题是要找到号码对应字符串的所有组合。用字典来表示数字到字符串的组合。然后遍历数字串。使其对应的字母list与前面已有的组合进行 连接即可。 19.Remove Nth Node ...
leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 ...242 Letter Combinations of a Phone Number 587 243 Restore IP Addresses 589 244 Reverse Integer 591 245 Palindrome Number 593
leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。 编号 中文题目 英文题目 题目描述 JavaScript 难度 1 Two Sum 简单 2 Add Two Numbers 中等 3 Longest Substring Without Repeating... 中等 5...
Leetcode扑克 Leetcode Starting from 2020/08/02 Leetcode practice with Python (for now) Daily Challenge + Selected Questions From Using Leetcode Plugin for Solutions ID English Title 中文题目名称 ...
leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** Leetcode 题目列表 005 Longest Palindromic Substring 006 ZigZag Conversion 007 Reverse Integer 008 ...
leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 already!) 我经常在递归的结束地方忘记return!!! 题型一:经典暴力递归:(里面涉及到重复不重复的...
题目来源:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number 题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。 给出数字到字母的映射如下(与电话按键相同)。注意 1 ...
Letter Combinations of a Phone Number 广度优先搜索 Word Ladder Word Ladder II Surrounded Regions 总结 深度优先搜索 Additive Number Palindrome Partitioning Unique Paths Unique Paths II N-Queens N-...
java lru leetcode Leetcode 问题的解决方案 问题 解决方案 0001_Two_Sum 0002_Add_Two_Numbers 0003_Longest_Substring_Without_Repeating_Characters ...0004_Median_of_Two_...0017_Letter_Combinations_of_a_Phone_N
加油站 leetcode ...17-letter-combinations-of-a-phone-number 20 cargo run --bin 20-valid-parentheses 21 cargo run --bin 21-merge-two-sorted-lists 27 cargo run --bin 27-remove-element 28
017_Letter_Combinations_of_a_Phone_Number 018_4总和 019_Remove_Nth_Node_From_End_of_List 020_Valid_Parentheses 021_Merge_Two_Sorted_Lists 022_Generate_Parentheses 023_Merge_k_Sorted_Lists 024_...