题面

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. PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)

    1044 Shopping in Mars (25 分)   Shopping in Mars is quite a different experience. The Mars people pay ...

  2. 无法连接App Store

    试了很多网上的方法,都没有效果,最后把hosts文件清空了,就可以了,不知道是为啥,同一份hosts文件在屋里能用,公司就不能用.

  3. vmware虚拟机网络不通原因之一

    我是在华硕笔记本上安装的vmware workstation.而且我用虚拟机的网络模式喜欢选“桥接”模式. 最近在虚拟上做实验,打开虚拟机windows 2003后,网卡配置静态ip后显示状态正常,但 ...

  4. 图形学入门(3)——区域填充算法(region filling)

    继续图形学之旅,我们已经解决了如何画线和画圆的问题,接下来要解决的是,如何往一个区域内填充颜色?对一个像素填充颜色只需调用SetPixel之类的函数就行了,所以这个问题其实就是:如何找到一个区域内的所 ...

  5. mysql的AB及读写和集群

    Mysql的AB及读写  第1章 Mysql的AB配置 1.1 master配置 1.2 slave配置 第2章 读写分离 2.1 安装mycat 2.2 启动mycat 2.3 登录mycat相关问 ...

  6. jqGrid取消所有选中

    // 获取所有选中行id var jqGridRowid=$("#jqGrid").jqGrid("getGridParam","selarrrow& ...

  7. 离线安装docker,并导入docker镜像

    将docker离线安装包导入到系统中,解压并进入文件夹,使用下述命令进行安装: rpm -ivh *.rpm --nodeps --force 安装完成功使用,docker info 查看docker ...

  8. Python爬虫入门教程之BeautifulSoup

    模块安装 pip3 install beautifulsoup4 模块导入 from bs4 import BeautifulSoup 示例html内容 RPC是一种比较流行的RPC通信框架,由谷歌公 ...

  9. IntelliJ IDEA 联想代码

  10. 《Tsinghua os mooc》第11~14讲 进程和线程

    第十一讲 进程和线程 进程 vs 程序 程序 = 文件 (静态的可执行文件) 进程 = 执行中的程序 = 程序 + 执行状态 进程的组成包括程序.数据和进程控制块 同一个程序的多次执行过程对应为不同进 ...