17. Letter Combinations of a Phone Number
来源:互联网 发布:mac tower 破解版下载 编辑:程序博客网 时间:2024/06/14 13:45
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.
思路1:利用一个递归遍历
unordered_map<char, string> data = { { '2', "abc" }, { '3', "def" }, { '4', "ghi" }, { '5', "jkl" }, { '6', "mno" }, { '7', "pqrs" }, { '8', "tuv" }, { '9', "wxyz" } };void dfs_letter(string &digits, int pos,string str, vector<string>& res){ if (str.size() == digits.size()){ res.push_back(str); return; } for (int i = 0; i < data[digits[pos]].size(); i++){ dfs_letter(digits, pos + 1, str + data[digits[pos]][i], res); }}vector<string> letterCombinations1(string digits) { vector<string> str; if (digits == "")return str; dfs_letter(digits, 0, "", str); return str;}
思路2:利用一个循环过程
unordered_map<char, string> data = { { '2', "abc" }, { '3', "def" }, { '4', "ghi" }, { '5', "jkl" }, { '6', "mno" }, { '7', "pqrs" }, { '8', "tuv" }, { '9', "wxyz" } };vector<string> letterCombinations(string digits) { vector<string> str; if (digits == "")return str; vector<string> v({ "" }); for (int i = 0; i < digits.size(); i++){ vector<string> temp; for (int j = 0; j < v.size(); j++){ for (int k = 0; k < data[digits[i]].size(); k++){ temp.push_back(v[j] + data[digits[i]][k]); } } v = temp; } return v;}
阅读全文
0 0
- 17.Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- java nio socket简单示例
- [一天几个linux命令] tail
- codevs1353 大神的游戏(快速幂)
- 深入浅析动态规划算法
- Tomcat内存溢出
- 17. Letter Combinations of a Phone Number
- 453. Minimum Moves to Equal Array Elements(unsolved)
- 使用 PackTextures
- 测试人员的类型
- 如何在eclipse里关联查看android源码
- 2017年第八届“蓝桥杯”国赛B组C/C++ 个人题解
- AVL树(高度平衡的二叉搜索树)平衡因子的调节和旋转
- 从零开始学_JavaScript_系列(36)——base64字符串与图片的相互转换
- 排序-插入排序-Java