Letter Combinations of a Phone Number
2017-04-05 14:42
351 查看
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.
题目考察深度优先遍历,给出了DFS算法
A mapping of digit to letters (just like on the telephone buttons) is given below.
题目考察深度优先遍历,给出了DFS算法
class Solution { public: vector<string> letterCombinations(string digits) { vector<string> ans; if(digits.size() == 0) return ans; int depth = digits.size(); string tmp(depth, 0); dfs(tmp, 0, depth, ans, digits); return ans; } void dfs(string &tmp, int curdep, int depth, vector<string> &ans, string &digits){ if(curdep >= depth){ ans.push_back(tmp); return ; } for(int i = 0; i < dic[digits[curdep] - '0'].size(); ++ i){ tmp[curdep] = dic[digits[curdep] - '0'][i]; dfs(tmp, curdep + 1, depth, ans, digits); } return ; } private: string dic[10] = {{""},{""},{"abc"},{"def"},{"ghi"},{"jkl"},{"mno"},{"pqrs"},{"tuv"},{"wxyz"}}; };
相关文章推荐
- leetcode 17. Letter Combinations of a Phone Number
- LeetCode 17.Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number(middle)
- [leedcode 17] Letter Combinations of a Phone Number
- Leetcode-Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode--Letter Combinations of a Phone Number(Python)
- LeetCode Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number--LeetCode
- Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number--LeetCode
- 17. Letter Combinations of a Phone Number
- 0017_Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- [LeetCode] 17. Letter Combinations of a Phone Number
- 个人记录-LeetCode 17. Letter Combinations of a Phone Number
- leetcode:Letter Combinations of a Phone Number