Letter Combinations of a Phone Number——简单的回溯算法
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.
class Solution {
private:
vector<string> res;
string temp;
public:
void getRes(string digits,int len,map<char,string> num_map,int index)
{
if(index>=len)
{
res.push_back(temp);
return;
}
char strNum=digits[index];
string strLetter=num_map[strNum];
for(int j=;j<strLetter.size();j++)
{
temp.push_back(strLetter[j]);
getRes(digits,len,num_map,index+);
temp.pop_back();
}
}
vector<string> letterCombinations(string digits) {
map<char,string> num_map;
num_map['']="";
num_map['']="";
num_map['']="abc";
num_map['']="def";
num_map['']="ghi";
num_map['']="jkl";
num_map['']="mno";
num_map['']="pqrs";
num_map['']="tuv";
num_map['']="wxyz";
int len=digits.size();
if(len==)
return res;
getRes(digits,len,num_map,);
return res;
}
};
Letter Combinations of a Phone Number——简单的回溯算法的更多相关文章
- [LeetCode]Letter Combinations of a Phone Number题解
Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...
- [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 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
随机推荐
- PLAN OF HEOI(unfinished)
Au:整体二分/计算几何/多项式/fwtAg:可持久化重量平衡树/线段树分治/线段树合并/最短路树/最短路DAGCu:三分Up:博弈论/置换群/杜教筛/矩阵树定理/BSGS/动态树分治/网络流(线性规 ...
- [zhuan]tomcat环境配置
http://jingyan.baidu.com/article/8065f87fcc0f182330249841.html 一.安装JDK和Tomcat 1,安装JDK:直接运行jdk-7-wind ...
- 006.C++头文件
1.引用头文件 标准头文件 #include <iostream> 自定义头文件 #include "complex.h" 2.防卫式(guard)声明 ...
- 南阳ACM 题目71:独木舟上的旅行 Java版
独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客的总 ...
- ZooKeeper概述(三)
ZooKeeper:分布式应用的分布协调服务 ZooKeeper是一个为分布式应用提供的分布的开源的协调服务.它暴露一组简单的原子操作,分布式系统可以在这之上为同步,配置管理,和组和命名实现更高级的服 ...
- java -classpath or -cp 的设置和解释
classpath is a parameter—set either on the command-line, or through an environment variable—that tel ...
- 「6月雅礼集训 2017 Day7」电报
[题目大意] 有n个岛屿,第i个岛屿有有向发射站到第$p_i$个岛屿,改变到任意其他岛屿需要花费$c_i$的代价,求使得所有岛屿直接或间接联通的最小代价. $1 \leq n \leq 10^5, 1 ...
- 51nod 1766 树上的最远点对——线段树
n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即你需要求出max{dis(i,j) |a<=i<=b,c<=j& ...
- 汕头市队赛SRM 20 T2不净的圣杯
不净的圣杯 SRM 20 背景 作为一张BUG级别的卡,官方打算把它修改得人畜无害一些…… 虽然名字还没想好,但是能力大概是对敌方所有单位造成d点伤害,d为自己牌组中所有卡的编号的最大公约数.这无疑是 ...
- MYSQL查找总结
a.条件判断where select * from 表 where id > 1 and name != 'alex' and num = 12; select * from 表 where ...