回溯法 17. Letter Combinations of a Phone Number
class Solution {
public:
map<char,string> dict;
vector<string> letterCombinations(string digits) {
dict[''] = "abc";
dict[''] = "def";
dict[''] = "ghi";
dict[''] = "jkl";
dict[''] = "mno";
dict[''] = "pqrs";
dict[''] = "tuv";
dict[''] = "wxyz";
vector<string> ans;
helper(digits, digits.size(), ans);
return ans;
}
// 先把前n-1个实现,然后在前面的结果中追加第n个数字对应的字母,就是最终的结果。
void helper(string &digits, int n, vector<string>& ans)
{
if(n == ) return;
//第n个数
char num = digits[n-];
if(n == )
{
for(auto c:dict[num])//对每个字母
ans.push_back(string(,c));
return;
}
//先获得前n-1个数字组成的字符串数组
vector<string> a;
helper(digits, n-, a);
for(auto c: dict[num])// 把第n-1个数字,追加到每一个
{
for(auto str: a)
{
str.push_back(c);
ans.push_back(str);
}
}
}
};
回溯就是递归。把大问题分解成小问题?不知道是怎么描述这个思想。
回溯法 17. Letter Combinations of a Phone Number的更多相关文章
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- Leetcode 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 17. Letter Combinations of a Phone Number C++回溯法
简单的回溯法! class Solution { public: void backTrack(string digits, vector<string> words, string an ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
- 17. Letter Combinations of a Phone Number[M]电话号码的字母组合
题目 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that ...
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
随机推荐
- visual studio 2017 中默认无法开发 Android 8.0 及以上系统的解决方案
一般默认比较旧有两个原因,系统版本过旧,Visual Studio 版本过旧. 第一步,将windows 更新到最新版,必须是windows 10 并且更新到最新. 第二步,将visual studi ...
- c标签 多个条件
<c:if test="${(rwyy01.yyry==NULL || rwyy01.yyry=='') && (rwyy01.shry==NULL || rwyy01 ...
- SoapUI简介及下载地址
SoapUI是一个开源测试工具,通过soap/http来检查.调用.实现Web Service的功能/负载/符合性测试.该工具既可作为一个单独的测试软件使用,也可利用插件集成到Eclipse,mave ...
- centos 7下rabbitmq安装(转)
安装erlang环境 添加rabbitmq依赖的erlang yum命令repos # In /etc/yum.repos.d/rabbitmq-erlang.repo [rabbitmq-erlan ...
- bash: ./LM35_make_fs: Permission denied 解决办法
执行命令的时候 ./LM35_make_fs 遇到 permission denied, bash: ./LM35_make_fs: Permission denied权限的问题,可以运行 ls -l ...
- java获取客户端ip地址工具类
public class IpUtils { private static final String[] HEADERS = { "X-Forwarded-For", " ...
- C#工具类:Json操作帮助类(转载)
原文转载自C#工具类:Json操作帮助类_IT技术小趣屋. Json序列化和反序列化在程序开发中时常会遇到,在C#中可以使用很多种方法实现对数据的Json序列化和反序列化,封装一个Json操作工具类来 ...
- mysql循环插入千万级数据
mysql使用存储过程循环插入大量数据,简单的一条条循环插入,效率会很低,需要考虑批量插入. 测试准备: 1.建表: CREATE TABLE `mysql_genarate` ( `id` ) NO ...
- 读取excel 文件到datatable
上一篇文章介绍了将datatable 内容导出到excel 文件,这里介绍如何将一个excel 文件读取出来,并保持到datatable 中,实际这样的应用场景也是经常遇到的. 这里继续使用了Micr ...
- traditional ajax提交数据有列表的时候需要添加
$.ajax({ url:'test', type:'POST', traditional:true, data: {'test':[1,2,3,4]} }) 提交数据有列表时需要使用traditio ...