《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
017. Letter Combinations of a Phone Number[M]
问题
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”].
分析
这题其实含义是是枚举所有可能的组合方式。如果题目要求是枚举“23”的所有可能字母组合,我们很好做,2重循环对吧?但是现在难就难在,一开始你不知道输入是什么,你没办法确定组合长度,组合个数,也没办法确定循环层数,这时候怎么办???
这里有两个思路,也是很常用的思路:递归,队列。
思路1:递归
递归一般是解决一些整体不好求的问题。它通过把大问题划小,然后找到一种特定的规律,然后求解。递归的思路我们很好理解,我们没办法确定整体,我可以先从入手。假定有个数字串“23456”
- 假定除了数字’2’外,后一串数字的组合我已经求出来了,那我只要把‘2’所代表的’abc’加到他们每一个的前面就好。所以现在只用求数字串”3456”
- 假定’3’之后一串数字的组合我已经求出来了,那我只要把‘3’所代表的’def’加到他们每一个的前面就好。所以现在只用求数字串”456”
……
- 一直这样推下去,直到发现6’后面是空的了,那返回它代表的每个字母就好了。
递归这里直接引用annafan的代码。
public class Solution {
public static List<String> letterCombinations(String digits) {
String digitletter[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
List<String> result = new ArrayList<String>();
if (digits.length()==0) return result;
result.add("");
for (int i=0; i<digits.length(); i++)
result = combine(digitletter[digits.charAt(i)-'0'],result);
return result;
}
public static List<String> combine(String digit, List<String> l) {
List<String> result = new ArrayList<String>();
for (int i=0; i<digit.length(); i++)
for (String x : l)
result.add(x+digit.charAt(i));
return result;
}
}
思路2:用队列
队列的思路也不算太难理解。如果递归算纵向求解的话,队列就是横向求解。每加入一个新的数字的时候,就把当前队列的元素全都扩充一遍。使得队列不仅在长度上,也在宽度上增加了。这就像一个装配流水线。半成品每流过一个工人,工人就把之前的产品拿出来,往上安装一个零件,然后放到传送带上,让它继续传到下个工人那。
- 一共需要的工人数,就是数字串长度,它决定了产品需要经过几道加工
for(int i = 0;i < digits.length(); i++)
{
}
- 然后我们看目前有多少个不同的半成品需要加工
int pos = digits.charAt(i) - '0';
int size = result.size();
for(int j = 0;j < size;j++)
{
}
- 然后就开始加工了,我们获取每个数字对应的字符串长度,这就是工人需要加工的零件个数。这里加工是把每个半成品拿出来,复制多份,然后按上新的零件
String tmp = result.remove();
for(int k = 0;k < map[pos].length();k++)
result.add(tmp+map[pos].charAt(k));
整体代码
public class Solution {
public List<String> letterCombinations(String digits) {
LinkedList<String> result = new LinkedList<String>();
if(digits.length() == 0)
return result;
String[] map = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
result.add("");
for(int i = 0;i < digits.length(); i++)
{
int pos = digits.charAt(i) - '0';
String s = map[pos];
int size = result.size();
for(int j = 0;j < size;j++)
{
String tmp = result.poll();
for(int k = 0;k < s.length();k++)
result.add(tmp+s.charAt(k));
}
}
return result;
}
}
这里,我用了size变量来存之前加工好的半成品个数(因为队列会在加工后扩充,size会变化),
int size = result.size();
for(int j = 0;j < size;j++)
但是,高分答案中有个思路,我觉得很赞。(要是想不到这个,就用我上面的写就好了,多一行代码而已)
while(ans.peek().length()==i)
这里ans.peek().length()是取出第一个元素的长度,当长度等于i的时候,说明是当前需要加工的半成品,而加工完后,队列中的每个元素长度都会增加1,所以,这时候循环就会停止。
《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]的更多相关文章
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
- leetcode个人题解——#17 Letter Combinations of a Phone Number
思路:用深搜遍历九宫格字符串,一开始做的时候发生了引用指向空地址的问题,后来发现是vector不能直接=赋值. class Solution { public: int len; ]={"a ...
- 【LeetCode】17. Letter Combinations of a Phone Number
题目: 思路:设置两个List,一个存储当前层,一个存储最终层 public class Solution { public List<String> letterCombinations ...
- LeetCode:17. Letter Combinations of a Phone Number(Medium)
1. 原题链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 题目要求 给定一 ...
- Leetcode 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
随机推荐
- struts2从浅至深(四)下载文件
1.创建下载文件动作类 2.配置struts 3.提供一个下载链接 4.下载页面 为什么文件名是链接名 只是以链接名显示,但文件的本身是个图片秩序改掉后缀名就可以了
- POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14165 Accepted: 4619 Descri ...
- Python学习-40.Python中的迭代
在上一篇中,我们使用了生成器来创建了一个可遍历的对象.在其中,我们使用了yield关键字. Python我也正在学习中,因此对yield的本质我并不熟悉,但是,在C#中,yield关键字则是语法糖,其 ...
- ASP.NET MVC 中单独的JS文件中获取Controller中设定的值
1,在Controller中的Action 中将指定值写上. // // GET: /Home/ public ActionResult Index() ...
- Spring Boot 2 实践记录之 封装依赖及尽可能不创建静态方法以避免在 Service 和 Controller 的单元测试中使用 Powermock
在前面的文章中(Spring Boot 2 实践记录之 Powermock 和 SpringBootTest)提到了使用 Powermock 结合 SpringBootTest.WebMvcTest ...
- Spring Boot 2 实践记录之 条件装配
实验项目是想要使用多种数据库访问方式,比如 JPA 和 MyBatis. 项目的 Service 层业务逻辑相同,只是具体实现代码不同,自然是一组接口,两组实现类的架构比较合理. 不过这种模式却有一个 ...
- C# 代码风格要求
一个.cs源文件至多定义两个类型 所有命名空间.类型名称使用Pascal风格(单词首字母大写),私有方法.受保护方法,仍使用Pascal风格命名 本地变量.方法参数名称使用Camel风格(首字母小写, ...
- [USACO5.1] 乐曲主题Musical Themes
题目链接:戳我 Emmm......hash怎么做啊不会啊 这里是SA后缀数组版本的 就是先两两做差分,作为要处理后缀的数组.普通地求出来h数组之后,我们二分这个答案,然后判定是否合法就行了.是否合法 ...
- 配置阿里yum源,设置命令
配置阿里yum源 #linux的软件包管理 安装 软件的方式有三种 .源代码编译安装() .下载python3的源代码 .解压缩源代码 .进入源代码目录,开始编译安装 .配置环境变量 .yum方式安装 ...
- jzoj5928
tj:題解裡公式是錯的 我們可以考慮每一個節點[a,a+2^b-1]對答案的貢獻 則當這個節點是左兒子時,貢獻為2^b 是右兒子時,貢獻為2n−a−2b+12^n-a-2^b+12n−a−2b+1 左 ...