题面

Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

(题面来自leetcode,包括图片)

我们知道电话按键每个数字都分别对应几个字母,现给定一串数字,找到所有可能的字符组合序列。

样例

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

样例求解过程

思路

本题暴力遍历是不可取的,参考上图,可采用DFS深度优先遍历递归来实现所有字母的组合。

Note: 要注意0和1的出现,没有字母与之对应,但需要考虑和处理。

DFS过程

 dfs(para1, para2, ...)
{
if()//满足状态,返回
{
....
return ;
}
//递归向下,继续搜索
for(....)
dfs(depth+, ....) ; return ;
}

源码

 class Solution {
public:
vector<string> letterCombinations(string digits) {
int depth = digits.length();
if(depth == )
return vector<string> {}; string tmp(depth, );
vector<string> res;
dfs(tmp, , depth, res, digits);
return res;
}
//使用引用,提高效率
void dfs(string &tmp, int curdeep, int depth, vector<string> &ans, string &digits)
{
if(curdeep >= depth)//长度够了,就返回
{
ans.push_back(tmp);
return ;
}
for(int i = ; i < dic[digits[curdeep]-''].length(); ++i)
{
tmp[curdeep] = dic[digits[curdeep]-''][i];
dfs(tmp, curdeep+, depth, ans, digits);//递归搜索
}
return ;
}
private:
string dic[] = {{""}, {""}, {"abc"}, {"def"}, {"ghi"}, {"jkl"}, {"mno"}, {"pqrs"}, {"tuv"}, {"wxyz"}};//数字对应字符字典
};

string+DFS leetcode-17.电话号码下的字母组合的更多相关文章

  1. Java实现 LeetCode 17 电话号码的字母组合

    17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...

  2. [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###

    描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[&q ...

  3. [LeetCode] 17. 电话号码的字母组合(回溯)

    题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[& ...

  4. [LeetCode] 17. 电话号码的字母组合

    题目描述:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/ 题目描述: 给定一个仅包含数字 2-9 的字符 ...

  5. LeetCode 17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    题目描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出: ...

  6. leetcode 17电话号码的字母组合

    与子集70?类似,子集每次两个分支,本题每次k个分支,子集是第一次不push第二次push元素,本题是每次都push元素,因此,本题答案的长度都为k,子集题目为各种组合: /** res,level, ...

  7. leetcode 17 电话号码的数字组合

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合.给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. class Solution { List<String ...

  8. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

  9. LeetCode-394. Decode String(DFS)

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

随机推荐

  1. 41 Flutter 仿京东商城项目签名验证 增加收货地址、显示收货地址 事件广播

    加群452892873 下载对应41课文件,运行方法,建好项目,直接替换lib目录 AddressAdd.dart import 'package:dio/dio.dart'; import 'pac ...

  2. 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_02-用户认证-认证服务查询数据库-需求分析&搭建环境

    1.2 认证服务查询数据库 1.2.1 需求分析 认证服务根据数据库中的用户信息去校验用户的身份,即校验账号和密码是否匹配. 认证服务不直接连接数据库,而是通过用户中心服务去查询用户中心数据库. 完整 ...

  3. 使用MockMvc进行springboot调试(SpringbootTest)

    测试前关闭web项目.springboot启动程序WebApplication.class 笔者本地自定了端口SpringBootTest.WebEnvironment.DEFINED_PORT 代码 ...

  4. Flask 应用如何部署

    1. Why Flask+Gunicorn+Nginx Flask+Gunicorn+Nginx是最常用的Flask部署方案,大家深究过为何用这样的搭配么? 1.1 Why? Flask 是一个web ...

  5. (二十)sql基础

    sql基础 --单表查询 select * from student; select * from score; --投影查询 select * from student; --条件查询 select ...

  6. fastJson工具类

    jar:fast.jar 依赖: <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId&g ...

  7. Mac安装7Z以及Mac下查看隐藏文件夹

    一:Mac下安装7Z: 1:brew直接安装解压工具 $ brew search 7z 会搜索到: ==> Formulae p7zip 2:$ brew install p7zip       ...

  8. laydate时间控件:开始时间,结束时间最大最小值

    时间控件地址及插件下载链接:https://www.layui.com/doc/modules/laydate.html 填充时间已两个功能为例: 1.添加功能 :时间 规则:选择开始时间后,点击结束 ...

  9. mysql主从同步原理及错误解决

    mysql主从同步的原理: 1.在master上开启bin-log日志功能,记录更新.插入.删除的语句. 2.必须开启三个线程,主上开启io线程,从上开启io线程和sql线程. 3.从上io线程去连接 ...

  10. HTTP网页请求状态码

    我们平时在打开一些网页的时候,会遇到打不开的情况,页面提示404错误,这个404就是http状态码.如果我们可以正常打开网页,这时也会有http状态码的,这个状态码就是200,只不过这时我们是无法通过 ...