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 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
随机推荐
- LN : leetcode 413 Arithmetic Slices
lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...
- jqueryUI插件
<link rel="stylesheet" href="~/Content/themes/base/jquery-ui.css" /> <s ...
- NavigationView的使用
代码已经分享至github:https://github.com/YanYoJun/NavigationDemo 转载请注明原文链接:http://www.cnblogs.com/yanyojun/p ...
- YOLO模型对图片中车辆的识别比对
1,模型对比结果 ² 标准Yolo v3模型 ² 标准Yolo v3 tiny模型 ² 标准Yolo v2 tiny模型 ² 用户训练yolo ...
- swiper移动端下不能正常轮播的解决方案-----此坑没躺过估计很难找到正确姿势
<script> var mySwiper = new Swiper('.swiper-container', { direction: 'vertical', //horizontal横 ...
- 迅为I.MX6开发板工业级嵌入式开发平台
迅为-i.MX6开发板是是基于ARM Cortex™-A9架构的高扩展性多核系列应用处理器, i.MX6系列芯片而且根据应用场合的不同,提供了可供选择的单核.双核和四核产品供客户选择.i.MX6系列的 ...
- swift -Dynamic Dispatch
These instructions perform dynamic lookup of class and generic methods. The class_method and super_m ...
- java_lock锁
lock锁是一个接口,jdk5.0新增的接口: 在线程中创建一个他的实现类对象Reentrantlock,默认为fals可以改为true,改为true后是有序的 把操作共享资源的代码放入try中,在t ...
- 【转载】关于 Google Chrome 中的全屏模式和 APP 模式
[来源于]新浪微博:@阿博 http://www.cnblogs.com/abel/p/3235839.html 全屏模式:kiosk 默认全屏打开一个网页呢,只需要在快捷方式中加上 --kiosk ...
- 02Hibernate基本配置
Hibernate基本配置 1.引入jar 2.建立项目 3.创建实体类 package com.sqlserver.domain; public class Customer { long cust ...