题面

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. osg::MatrixTransform 模型基本变换

    VCNodeMatrix.h #pragma once #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHand ...

  2. 不同操作系统上DNS客户端操作区别汇总

    结论:windows有DNS缓存,Linux默认无DNS缓存,只能依赖于安装其他软件. 一.不同操作系统的客户端的DNS缓存差别 1.windows 系统中dns 解析器会使用系统的dns缓存来提高d ...

  3. git clone时加上--depth 1

    当项目过大时,git clone时会出现error: RPC failed; HTTP curl The requested URL returned error: Gateway Time-out的 ...

  4. iis启动异常 0x80072749

    错误提示: “/”应用程序中的服务器错误. 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同 ...

  5. PHP重建数组的索引

    sort() array_merge()跟一个空数组合并都可以重建索引数组的键(key)

  6. ubuntu 18.04安装mysql 8

    wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.10-1_all.deb sudo dpkg -i mysql-apt-config_0. ...

  7. Paper reading: High-Fidelity Pose and Expression Normalization for Face Recognition in the Wild(HPEN)

    1. Introduction 人脸识别受到各种因素影响,其中最重要的两个影响是 pose 和 expression, 这两个因素会对 intra-person 变化产生极大的影响, 有时候甚至会超过 ...

  8. [CareerCup] Guards in a museum 博物馆的警卫

    A museum was represented by a square matrix that was filled with O, G, and W where O represented ope ...

  9. VS2010开发.cpp与.c的注意事项

    VS2010开发.cpp与.c的注意事项 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 强大的VS2010,正是由于vs2010的完全封装,让现在的wind ...

  10. Rhino脚本引擎技术介绍

    引用:http://p.primeton.com/articles/54c1e255be20aa4735000001 http://blog.csdn.net/u013292493/article/d ...