leetcode 第17题 Letter Combinations of a Phone Number
来源:互联网 发布:家装网络销售好做吗 编辑:程序博客网 时间:2024/05/20 18:48
一下子看到这个题目并没有什么思路,但是看着好像又有点眼熟,有点类似于全排列的感觉,看了题目解析之后才恍然大悟,这就是我学习过的深度优先搜索算法呀,全排列就可以用这种方法。这个题目的思路整理如下:
1、需要注意的是,当参数string digits的长度为K时,最终子串的长度就是K,这是递归调用的返回条件(也就是相当于完成了一种组合),即当输入"23",长度为2,则结果集中的"ad""ae""af"等长度都是2;
2、基于这个条件,我们对输入的数字串进行递归调用,每次往临时子串变量temp中,添加当前数字对应的字串中的一个字母,并且每当完成一组子串后,将其添加到结果集的同时,将临时变量temp弹出上一个字符,深度优先搜索;
说明一下深度优先搜索的意义:深度优先搜索的关键在于“当下该如何做”,至于“下一步如何做”则与“当下该如何做”是一样的。在这个题目中也是一样的,遇到第一个数字的时候应该for循环一下把每一种可能都尝试一遍,当前这一步解决之后就进入下一步,也就是遇到第二个数字的时候,这一步的解决方法和上一步的解决方法是完全一样的,下面是深度优先搜索的基本模型:
void dfs(参数){ 判断边界; 尝试每一种可能 for(i=1;i<n;i++){ 继续下一步 dfs(相同的参数); } 返回}参考代码:
class Solution{public: vector<string> letterCombinations(string digits){vector<string> result;string temp = "";if(digits.size() < 1)return result;map<char,string>myMap;myMap['2'] = "abc";myMap['3'] = "def";myMap['4'] = "ghi";myMap['5'] = "jkl";myMap['6'] = "mno";myMap['7'] = "pqrs";myMap['8'] = "tuv";myMap['9'] = "wxyz";MyFunc(digits, temp, result, myMap);return result;}private: void MyFunc(string digits, string temp, vector<string>& result, map<char, string>myMap){int currLen = temp.size();if(currLen == digits.size()){result.push_back(temp);}else{for(int i = 0; i < myMap[digits[currLen]].size(); i++){temp = temp + myMap[digits[currLen]][i];MyFunc(digits, temp, result, myMap);temp.pop_back();}}}};
阅读全文
0 0
- leetcode 第17题 Letter Combinations of a Phone Number
- LeetCode 第17题:Letter Combinations of a Phone Number
- 【LeetCode】LeetCode——第17题:Letter Combinations of a Phone Number
- leetcode第17题 17. Letter Combinations of a Phone Number
- leetcode第17题——**Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- LeetCode | Letter Combinations of a Phone Number
- Scrapy爬虫中使用Splash抓取动态JS页面
- [dynamic FL]part 6:comparison between Dynamic and Static
- 吝啬的国度
- 获取手机中应用程序
- VirtualBox 安装 linux后 一些设置
- leetcode 第17题 Letter Combinations of a Phone Number
- snapdragon连接、使用总结
- List < String[] > 转成数组后是二维数组
- JumpServer 安装部署与试用心得
- Python类方法、静态方法、全局变量的使用
- HTML5相关资源分享
- 【iOS】Category VS Extension 原理详解
- 带头结点的单链表上将后k个结点变换为前k个结点的操作
- iOS-时间戳