回溯法 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. ...
随机推荐
- 自己DIY出来一个JSON结构化展示器
说来也巧,这个玩意,一直都想亲手写一个,因为一直用着各种网上提供的工具,觉得这个还是有些用途,毕竟,后面的实现思路和原理不是太复杂,就是对json的遍历,然后给予不同节点类型以不同的展现风格. 我这次 ...
- Mxd文档更新比例尺
在AE中,更新Mxd文档的比例尺,比较特殊.写代码以记录,更新比例尺代码如图所示: [DllImport("User32.dll")] public static extern i ...
- 【转】META标签指南:哪些meta标签该用哪些不该用?
以下内容来源:http://lusongsong.com/reed/8766.html META标签是网页代码中HEAD区的一个关键标签,其提供的信息虽然用户不可见,但却是文档的最基本的元信息.说起m ...
- 远程访问服务器tensorboard
转自https://blog.csdn.net/Hansry/article/details/83004648 1. 在登录远程服务器的时候使用命令: : server_name@server.add ...
- redis 中的key值过期后,触发通知事件
1.创建springboot工程,创建监听类 maven配置 <dependencies> <dependency> <groupId>org.springfram ...
- WebApi设置HttpContext.Current.User
1.Web.config配置上system.web节点下加入以下配置 <system.web> <authentication mode="Forms"> ...
- LeetCode 789. Escape The Ghosts
题目链接:https://leetcode.com/problems/escape-the-ghosts/description/ You are playing a simplified Pacma ...
- shell编程变量介绍与表达式详解
shell变量简介 变量是任何一种编程语言都必不可少的组成部分,变量用来存放各种数据.脚本语言在定义变量时通常不需要指明类型,直接赋值就可以,Shell 变量也遵循这个规则. 在 Bash shell ...
- SUID、SGID详解
文章转载至:http://tech.ccidnet.com/art/2583/20071030/1258885_1.html 如果你对SUID.SGID仍有迷惑可以好好参考一下! Copyright ...
- flutter 入口文件配置路由+加载页面
入口文件配置路由 1.路由信息 -- 加载页面 ,通常用于显示新的内容或者广告,加载完成之后进入主页面 -- 主页面 /app 2.配置页面 main.dart main.dart // main ...