Letter Combinations of a Phone Number(带for循环的DFS,组合问题,递归总结)
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.
又加深了对DFS的认识,在递归函数中,需要把握几个点,总能各个击破。
1.就是变化的量要作为参数来传递,这样也就是每个函数在自己的栈中都有该局部变量,这样就可以在回溯到某个点的时候,他的局部变量不会消失。
2.一般的终止条件中,计算结果
代码:
private:
map<char, vector<char> > dict;
vector<string> ret;
int len;
public:
void createDict()
{
dict.clear();
dict[''].push_back('a');
dict[''].push_back('b');
dict[''].push_back('c');
dict[''].push_back('d');
dict[''].push_back('e');
dict[''].push_back('f');
dict[''].push_back('g');
dict[''].push_back('h');
dict[''].push_back('i');
dict[''].push_back('j');
dict[''].push_back('k');
dict[''].push_back('l');
dict[''].push_back('m');
dict[''].push_back('n');
dict[''].push_back('o');
dict[''].push_back('p');
dict[''].push_back('q');
dict[''].push_back('r');
dict[''].push_back('s');
dict[''].push_back('t');
dict[''].push_back('u');
dict[''].push_back('v');
dict[''].push_back('w');
dict[''].push_back('x');
dict[''].push_back('y');
dict[''].push_back('z');
}
void dfs(int loc,string digits,string temp)
{
if(loc==len){
ret.push_back(temp);
//temp.erase(temp.size()-1);在这里擦除是没有用的,这已经是下一个递归函数,回溯到上一个的时候,它的局部变量temp还是三个字母
return;
}
for (int i=;i<dict[digits[loc]].size();++i)
{
temp.push_back(dict[digits[loc]][i]);
dfs(loc+,digits,temp);
temp.erase(temp.size()-);
}
}
vector<string> letterCombinations(string digits) {
createDict();
vector<string> tempres;
tempres.push_back("");
if(digits.empty()) return tempres;
len=digits.size();
dfs(,digits,"");
return ret;
}
};
int main()
{
//freopen("C:\\Users\\Administrator\\Desktop\\a.txt","r",stdin);
Solution so;
so.letterCombinations("");
return ;
}
Letter Combinations of a Phone Number(带for循环的DFS,组合问题,递归总结)的更多相关文章
- 24.Letter Combinations of a Phone Number(电话号对应的字符组合)
Level: Medium 题目描述: Given a string containing digits from 2-9 inclusive, return all possible lette ...
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 69. Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- Letter Combinations of a Phone Number:深度优先和广度优先两种解法
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- leetcode-algorithms-17 Letter Combinations of a Phone Number
leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
随机推荐
- css3中的变换、动画和过渡
变换:分为2d变换和3d变换,但一次只能用一个变换属性,多个的话最后一个会覆盖前面所有最终被浏览器实现,变换可以成为过渡和动画的一个待变参数(类似width,opacity等). 过渡:是动画的初始模 ...
- 《基于Node.js实现简易聊天室系列之环境搭建》
前文提到了Demo所涉及的技术,现在讲环境(工具)的配置.环境的配置主要是数据库mongDB和Node.js的配置. Node.js Node.js的官方地址:https://nodejs.org/e ...
- R in action读书笔记(9)-第八章:回归 -回归诊断
8.3回归诊断 > fit<-lm(weight~height,data=women) > par(mfrow=c(2,2)) > plot(fit) 为理解这些图形,我们来回 ...
- R in action读书笔记(4)-第六章:基本图形(下)
6.3直方图 hist() 其中的x是一个由数据值组成的数值向量.参数freq=FALSE表示根据概率密度而不是频数绘制图形.参数breaks用于控制组的数量.在定义直方图中的单元时,默认将生成等距切 ...
- vue ---- Object的一些常用的方法
在对象上添加新属性的几种方法: 直接附代码: 法一:Es6扩展运算符添加属性 法二:利用语法Object.assign(target, ...sources) target目标对象.source ...
- Fragment中获取Activity的Context (转)
Fragment中获取Activity的Context时只需要this.getActivity()即可. 而不是许多人说的this.getActivity().getApplicationCo ...
- raw cannot be resolved or is not a field解决办法
解决raw文件夹问题 查看左侧项目/res文件夹下是否有raw文件夹,(一定是放到res文件夹下,raw在项目开始创建时候不会自动创建,所以要自己创建)
- win7 快捷键 收集
1. 轻松访问键盘快捷方式 按住右 Shift 八秒钟:启用和关闭筛选键 按左 Alt + 左 Shift + PrtScn (或 PrtScn):启用或关闭高对比度 按左 Alt + 左 Shift ...
- 结对项目--黄金点游戏(邓乐&曾亮)
#include<stdio.h> #include<stdlib.h> #include<Windows.h> int result[100][1000000]; ...
- patest_1007_Maximum Subsequence Sum_(dp)(思维)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...