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.

分析:首先这题我没写出来,感觉脑袋有点乱,思路上肯定是没错的,从头开始循环,然后加一个,再循环,再加再循环。这样的话可以用递归,但是总是想不出怎么递归,就直接看了别人的解答,很巧妙的一个思路。我想的是从第一个数字开始循环,然后加上去,存到list,但是这样就会出现一个问题,那就是如果直接对list里面的字符串操作的话后面又会加上新的,这些新的不好加。而别人的思路就是直接把list里面的拿出来,一个个循环。这里用到了list的peek和remove的区别,peek,取链表第一个元素,但是不删除,remove,取链表第一个元素,但是会删除。remove的是从头remove,而add是从尾add,这样的话新的字符串就都在list后面了,所以可以以list头的字符串长度来作为是否完成一次添加的标准。

public static  List<String> letterCombinations(String digits) {
LinkedList<String> ans = new LinkedList<String>();
if(digits.isEmpty()) return ans;
String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
//防止第一个取length空指针
ans.add(""); for(int i=0;i<digits.length();i++) {
int x=digits.charAt(i)-48;
//当头元素长度也满足时表示整个表都满足,因为remove从头,add从尾巴。
while(ans.peek().length()==i) {
String t=ans.remove();
ans.remove(t);
for(char s:mapping[x].toCharArray()) {
ans.add(t+s);
}
}
}
return ans;
}

Leetcode 17.——Letter Combinations of a Phone Number的更多相关文章

  1. 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 ...

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

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

  3. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  4. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  5. [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合

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

  6. [LeetCode] 17. Letter Combinations of a Phone Number ☆☆

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

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

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

  8. LeetCode——17. Letter Combinations of a Phone Number

    一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...

  9. LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

    题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...

随机推荐

  1. TypeError: Error #1034: 强制转换类型失败:无法将 mx.controls::DataGrid@9a7c0a1 转换为 spark.core.IViewport。

    1.错误描述 TypeError: Error #1034: 强制转换类型失败:无法将 mx.controls::DataGrid@9aa90a1 转换为 spark.core.IViewport. ...

  2. 【HAOI2015】树上操作(树链剖分)

    题面 Description 有一棵点数为N的树,以点1为根,且树点有边权.然后有M个操作,分为三种: 操作1:把某个节点x的点权增加a. 操作2:把某个节点x为根的子树中所有点的点权都增加a. 操作 ...

  3. [POI2010]CHO-Hamsters

    KMP暴力求出next数组后 实际上是一个最短路问题,floyed搞一搞 然而会TLE 矩阵优化一下即可(倍增floyed) KMP在弱数据下可以AC..正解请看其他人博客 # include < ...

  4. TypeScript入门知识五(面向对象特性二)

    1.泛型(generic) 参数化的类型,一般用来限制集合的内容 class Person { constructor(private name: string) { } work() { }}var ...

  5. 用js实现左右阴影的切换

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  6. 为wampserver 添加新版本php支持

    1.1.  停止WAMP服务器. 2.下载要安装的PHP版本.下载Window版本的ZIP包啦:http://windows.php.net.解压到 Wamp的安装目录\bin\php\php7.2. ...

  7. handsontable 属性汇总

    常规属性: 1.固定行列位置 fixedRowsTop:行数 //固定顶部多少行不能垂直滚动 fixedColumnsLeft:列数 //固定左侧多少列不能水平滚动 2.拖拽行头或列头改变行或列的大小 ...

  8. Golang的CSP很酷?其实.NET也可以轻松完成

    说起Golang(后面统称为Go),就想到他的高并发特性,在深入一些就是 Goroutine.在大家被它优雅的语法和简洁的代码实现的高并发程序所折服时,其实C#/.NET也可以很容易的做到.今天我们来 ...

  9. adobe media encoder cc 2015在win10中打开崩溃的解决办法(该方法同样适用于adobe其他产品)

    今天就给大家讲讲adobe media encoder cc 2015启动的时候崩溃的问题,先来看看现象.就是这样了,然后我在网上找了很多办法,有的方法已经过时了,也或者因为现在新版本的adobe m ...

  10. EXCEL VLOOKUP函数怎么返回多列结果

    一般VLOOKUP函数只能返回一列的结果,本例介绍如何一次性返回多列结果.   工具/原料   Excel 函数使用方法说明:     首先,原始数据包括姓名.工号.性别和籍贯信息.现在需要根据姓名同 ...