回溯法 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. ...
随机推荐
- 16路PWM输出的pca9685模块
今天要介绍的就是该模块,该模块是16路pwm模块,使用I2C总线可以控制16路舵机(led). 接线OE空着就可以,其他VCC是芯片供电+5,SCL时钟线,SDA信号线,GND地线. 芯片介绍可以看: ...
- Thing in java 第四章,控制执行流程,练习题答案
/** * Created by Sandy.Liu on 2018/7/19. * Thinking in java, version 4, chapter 4, practice 1 * Writ ...
- Git切换远程分支
1. 切换git远程分支,使用命令:git checkout -b 分支名称. 注意:切换远程分支一定要带伤-b 参数,只有切换本地分支的时候才不需要 -b参数,-b 的意思是 bas ...
- Spark分布式编程之全局变量专题【共享变量】
转载自:http://www.aboutyun.com/thread-19652-1-1.html 问题导读 1.spark共享变量的作用是什么?2.什么情况下使用共享变量?3.如何在程序中使用共享变 ...
- update_engine-DownloadAction(二)
在update_engine-DownloadAction(一)中对DownloadAction介绍到了DeltaPerformer的Write方法.下面开始介绍Write方法. src/system ...
- ELK实践-Kibana定制化扩展
纵观任何一家大数据平台的技术架构,总少不了ElasticSearch:ES作为溶合了后端存储.快速检索.OLAP分析等功能的一套开源组件,更绝的是提供了一套集数据采集与前端展现为一体的框架(即ELK) ...
- 开源在线分析诊断工具Arthas(阿尔萨斯)--总结
阿里重磅开源在线分析诊断工具Arthas(阿尔萨斯) arthas用法 启动demo java -jar arthas-demo.jar 启动 java -jar arthas-boot.jar at ...
- # 20175311 2018-2019-2 《Java程序设计》第2周学习总结
## 教材学习内容总结 第二周我对如何运行java程序已经比较熟悉了,第二周更多的是注重程序内部的原理了. ## 教材学习中的问题和解决过程 - 问题1:看书时看到的一个例子,不是很懂它是怎么得出结果 ...
- C&C++类型定义typedef
1.声明 1.1结构声明: struct { int n; double x,y; }; 1.2带结构标志的声明 struct point{ double x,y; }; 1.3定义结构类型 1.3. ...
- 欢迎来到GitHub世界
什么是GitHub GitHub(Pronunciation:/githʌb/) 这是一个为开发者提供Git仓库的托管服务,这是一个让开发者们共享代码的完美场所.GitHub公司总部位于美国旧金山, ...