17. Letter Combinations of a Phone Number LeetCode
2016-03-15 22:49
495 查看
题意:给出9键手机键盘和用户按的数字,求所有可能的字母组合,按字典序排列。
题解:把数字和字母用map存一下,然后dfs就可以。
题解:把数字和字母用map存一下,然后dfs就可以。
class Solution { public: map<char,string>ma = {{'2',"abc"}, {'3',"def"}, {'4',"ghi"},{'5',"jkl"}, {'6',"mno"}, {'7',"pqrs"}, {'8',"tuv"}, {'9',"wxyz"}}; void dfs(vector<string>& ans,string pre,int k,string digits) { if(k == digits.length()) { ans.push_back(pre); return; } string tmp = ma[digits[k]]; for(int i = 0; i < tmp.length(); i++) dfs(ans,pre + tmp[i],k + 1,digits); } vector<string> letterCombinations(string digits) { vector<string> ans; ans.clear(); if(digits.length() == 0) return ans; dfs(ans,"",0,digits); return ans; } };
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解