leetcode第18题--Letter Combinations of a Phone Number
Problem:
Given a digit string, 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.
![]()
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
想了半天不知道怎么做,对递归真心不熟悉。特意看了算法导论的两个优先搜索,即广度优先和深度优先 bfs 和 dfs,这题用到的是dfs方法。
void dfs(int len, string s, string t, vector<string> &ans)
{
string ss[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
if (len == s.size()) // 当t已经存够s长度个的时候就可以push到答案中并返回
{
ans.push_back(t);
return;
}
for (int i = ; i < ss[s[len] -''].size(); ++i) // 进行深度优先搜索,len相当于depth
{
dfs(len + , s, t + ss[s[len] - ''][i], ans);
}
}
vector<string> letterCombinations(string digits)
{
vector<string> ans;
ans.clear();
if (digits.size() == )
{
ans.push_back("");
return ans;
}
dfs(, digits, "", ans);// 从第零层temp str 为空开始往深处搜索
return ans;
}
leetcode第18题--Letter Combinations of a Phone Number的更多相关文章
- LeetCode (17)Letter Combinations of a Phone Number
题目 Given a digit string, return all possible letter combinations that the number could represent. A ...
- leetcode个人题解——#17 Letter Combinations of a Phone Number
思路:用深搜遍历九宫格字符串,一开始做的时候发生了引用指向空地址的问题,后来发现是vector不能直接=赋值. class Solution { public: int len; ]={"a ...
- LeetCode(17)Letter Combinations of a Phone Number
题目如下: Python代码: class Solution(object): def letterCombinations(self, digits): """ :ty ...
- 乘风破浪:LeetCode真题_017_Letter Combinations of a Phone Number
乘风破浪:LeetCode真题_017_Letter Combinations of a Phone Number 一.前言 如何让两个或者多个集合中的随机挑选的元素结合到一起,并且得到所有的可能呢? ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
随机推荐
- Java魔法堂:注解用法详解——@SuppressWarnings(转)
一.前言 编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的“感叹号”就严重阻碍了我们判断该行是否设置的断点了.这时我们可以在方法前添加 @SuppressWar ...
- Forbidden You don't have permission to access / on this server.
原文:Forbidden You don't have permission to access / on this server. Forbidden You don't have permissi ...
- 浙江大学PAT考试1069~1072(2013-11-2)
11 题目地址:http://pat.zju.edu.cn/contests/pat-a-practise 1069: 由下降序和上升序两个四位数不断相减,然后得到新数据,始终会到达一个数字终止. 递 ...
- ActiveMQ与RabbitMQ采用camel综合
著名EIP实施框架Camel它起源于ActiveMQ的一些基于消息的集成需求.然后逐渐发展成为一个ActiveMQ的子项目,最后这一块的功能越来越完好.就成为了Apache的顶级项目. 所以,从一開始 ...
- JS数据绑定模板artTemplate试用
之前写JS绑定数据曾经用过tmpl库,虽然功能比较强大但是感觉不是很轻量,对于相对简单的数据需求显得有些臃肿.而Ajax返回数据自己拼接html的方式又显得不够高端,因此今天看了一篇介绍artTemp ...
- Binary Tree Maximum Path Sum [leetcode] dp
a(i):在节点i由于单边路径的最大结束 b(i):在节点i路径和 a(i) = max{ i->val, i->val + max{a(i->left), a(i->righ ...
- js中document.write()使用方法
<script> var hrf = window.location.href; if(hrf.indexOf('change')>0){ ...
- kendo ui 单击取消编辑数据grid减少的原因和治疗方法的数据
kendo ui单击取消编辑数据buttongrid数据缩减.原因grid编辑数据追打datasorce于data寻找阵列数据的存在.假定有不会加入,加入缺席. 首先一个样本: html代码: < ...
- 1023 Train Problem II(卡特兰数)
Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...
- XSS学习笔记(五)-XSS防御
如果只生产XSS的地方都与输入或输出相关联的.所以错过了主要矛盾.而且,我们将有一个解决问题的办法:您可以输入端砚格过滤,是可能的过滤输出时间,输出到用户的GET或POST中是否有敏感字符: 输入过滤 ...