回溯法 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. ...
随机推荐
- Python3的List操作和方法
列表函数: len(list):列表元素个数 max(list):返回list中最大的元素 min(list):返回list中最小的元素 list(seq):将元组转换为列表 列表方法: list.a ...
- 2018-2019-2 网络对抗技术 20165308 Exp4 恶意代码分析
2018-2019-2 网络对抗技术 20165308 Exp4 恶意代码分析 实验过程 一.系统运行监控 (1)使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一 ...
- 出现No package gcc+ available解决办法
系统 CentOS Linux release 7.4.1708 (Core) 安装gcc时报错 [root@ip---- node-v10.15.3]# yum -y install gcc+ ...
- 洛谷 4115 Qtree4——链分治
题目:https://www.luogu.org/problemnew/show/P4115 论文:https://wenku.baidu.com/view/1bc2e4ea172ded630b1cb ...
- CentOS7中KVM虚拟机内存、CPU调整
CentOS7中KVM虚拟机内存.CPU调整 1. 调小虚拟机内存 调小虚拟机内存可以动态实现,不用关机 1.1 查看当前内存大小 [root@kvm01 ~]# virsh dominfo vm1- ...
- Unity外包团队:U3D与UE我选哪个好?请别再问这种问题了!
原本预先决定的两家VR游戏公司采访,思熊和星为棋,并没有发现什么共性之初.结果在采访之后却意外发现,两家的经历有着非常相似的地方.他们都是来自于开发游戏所用的引擎的原开发商,比如思熊的主力来自Epic ...
- Ntrip协议简介(转)
原文地址:https://blog.csdn.net/sinat_19447667/article/details/67637167 1 什么是Ntrip? CORS(Continuously Ope ...
- Webpack 模块处理
webpack模块处理 1. ES6 静态Import ES6的import会被转化为commonjs格式或者是AMD格式,babel默认会把ES6的模块转化为commonjs规范的. import ...
- GNU make 汇总
= 是最基本的赋值 := 是覆盖之前的值?= 是如果没有被赋值过就赋予等号后面的值+= 是添加等号后面的值 $@--目标文件,$^--所有的依赖文件,$<--第一个依赖文件 makefile获取 ...
- [UE4]引用Grabbable接口
一.当前:可抓取对象的类型是GrabTargetActor 二.修改目标:可抓取对象的类型改成Grabbable. 1.Fand Grab Target的返回值改成Grabbale(变量的数据类型可以 ...