(M)Backtracking:17. Letter Combinations of a Phone Number
来源:互联网 发布:mac在线安装系统 编辑:程序博客网 时间:2024/06/01 17:22
这道题让我们求电话号码的字母组合,即数字2到9中每个数字可以代表若干个字母,然后给一串数字,求出所有可能的组合。
我想到类似全排列那样的题目,这道题也是典型的回溯问题,每一个位置都有几个选择,求这些选择的全排列。但是以前的题目是,这些位置有一个共同的选择范围,一般是一个数组。但是这个题是,每个位置有每个位置各自不同的选择范围。其实还是一样的,只是要设一个字典,再设一个level表示现在是在哪一个位置上做选择,然后找到这个位置对应的选择范围即可。和之前的题目一样的。
class Solution {public: vector<string> letterCombinations(string digits) { if(digits.size() == 0) return {}; vector<string> dict = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; vector<string> res; string s; backtracking(digits, dict, s, res, 0); return res; } void backtracking(string digits, vector<string>& dict, string& s, vector<string>& res, int level) { if(level == digits.size()) { res.push_back(s); return; } string dictstr = dict[digits[level] - '2']; for(auto a : dictstr) { s.push_back(a); backtracking(digits, dict, s, res, level+1); s.pop_back(); } }};
阅读全文
0 0
- (M)Backtracking:17. Letter Combinations of a Phone Number
- Backtracking: 17. Letter Combinations of a Phone Number
- LeetCode[Backtracking]: 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
- ocos2dx-lua采用pbc解析protobuf 不支持message嵌套和枚举不支持整数的处理方法
- Python爬取百度百科,BeautifulSoup提取关键信息
- python 字典
- HDU 1863 畅通工程(prim最小生成树)
- 【游记】记NOIP2017滚粗之旅
- (M)Backtracking:17. Letter Combinations of a Phone Number
- XAMPP安装配置教程(Windows)
- C++学习笔记——函数的参数传递
- 添加STC的MCU進Keil4
- 页面之间的通信
- 也来分析下Java 线程池(ThreadPoolExecutor)的原理
- MyEclipse快捷键有参构造和无参构造
- 一些工具的安装
- 数据加密