就dfs吧....

然后,我傻了。前一道题不用考虑空,这道题就要考虑。。。。

还有注意vector要引用传递

class Solution {

public:
void dfs(string temp,vector<string>&ret,string digits,int index,string* str,int len)
{
if(index==len)
{
ret.push_back(temp);
return;
}
else
{
int n=str[(digits[index]-'2')].length();
for(int i=0;i<n;i++)
dfs((temp+str[(digits[index]-'2')][i]),ret,digits,index+1,str,len);
}
} public:
vector<string> letterCombinations(string digits) {
vector<string>ret;
if(digits=="")
return ret;
string str[10] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int len=digits.length();
string temp="";
this->dfs(temp,ret,digits,0,str,len);
return ret;
}
};

另外有点震惊:这是机器性能太强还是测试用例太少奥

leetcode17 电话号码的字母组合 dfs的更多相关文章

  1. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  2. [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  3. Java实现LeetCode17. 电话号码的字母组合

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

  4. LeetCode17.电话号码的字母组合 JavaScript

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

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

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

  6. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

    题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...

  8. LeetCode(17):电话号码的字母组合

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

  9. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

随机推荐

  1. Promise.all()使用实例

    一.什么是Promise.all()? 在说这个之前要先说清楚promise.promise就是一个对象,专门用来处理异步操作的. 而Promise.all方法用于将多个 Promise 实例,包装成 ...

  2. SpringBoot @Value 解析集合配置

    引自:https://jitwxs.cn/d6d760c4.html 一.前言 在日常开发中,经常会遇到需要在配置文件中,存储 List 或是 Map 这种类型的数据.Spring 原生是支持这种数据 ...

  3. js 浮点数陷阱

    众所周知,JavaScript 浮点数运算时经常遇到会 0.000000001 和 0.999999999 这样奇怪的结果,如 0.1+0.2=0.30000000000000004.1-0.9=0. ...

  4. OpenStack使用OVN

    1. Controller节点 1.1 安装 OVS和OVN 安装 Python3.7: yum -y groupinstall "Development tools" yum - ...

  5. 配置HDFS的HA

    配置前准备: -- 配置hadoop -- 配置ZooKeeper,传送门:https://www.cnblogs.com/zhqin/p/11906106.html 安装配置好hadoop和ZooK ...

  6. http2 http1 对比

    RFC 7540 - Hypertext Transfer Protocol Version 2 (HTTP/2) https://tools.ietf.org/html/rfc7540#page-4 ...

  7. vim 行号的显示与隐藏

    通常我们在使用vim编辑器的时候,需要显示和隐藏行号 隐藏行号: 1.首先我们vim   1.txt   (进入我们编辑的文档),如下,此时是显示行号的 2.按一下esc键,并输入:(冒号),完成效果 ...

  8. LOJ10129

    AHOI 2009 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为 nn 的数列,不妨设为 a1​,a2​,⋯,an​.有如下三种操作形式: 把数列中的一段数全部乘一个值: 把 ...

  9. PowerBI系列组件关系详解

    随着数据分析工具的不断更新,我们所熟知的Excel可能已经不是你想象中的样子了. Excel和Power BI又有何千丝万缕的联系? M语言和DAX语言又是什么样的存在? 操作他们又需要掌握什么样的技 ...

  10. lodash的debounce函数的使用

    最新,在react新项目的开发中使用到了lodash类库的debounce方法,就随手梳理了一下此方法的方便之处 未使用debounce之前 如果不考虑使用debounce,那么在用户连续点击的情况之 ...