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. ASP.NET CORE的Code Fist后Models更改了怎么办?

    上次我写到MVC的code fist后,自动生成数据库并自动生成web页面了 点击打开链接 那么随着项目需求的逐步明确,model变化了怎么办呢?其实和上次一样的,有两条关键的语句要记住 Add-Mi ...

  2. springtest-junit-jidi--测试接口

    一,问题分析 假如在一个项目中要测试一个接口,但是这个项目没有对外开放url地址,话句话就是说浏览器访问不了里面的资源,自己造的数据不能测试接口,那么只能等别人来调用自己的接口,而且自己也不能测试,假 ...

  3. Python编程核心内容之一——Function(函数)

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 截至上篇随笔<Python数据结构之四--set(集合)>,Python基础知识也介绍好了.接下来准备干 ...

  4. 【BZOJ4237】稻草人(CDQ分治,单调栈)

    [BZOJ4237]稻草人(CDQ分治,单调栈) 题面 BZOJ 题解 \(CDQ\)分治好题呀 假设固定一个左下角的点 那么,我们可以找到的右下角长什么样子??? 发现什么? 在右侧是一个单调递减的 ...

  5. 【BZOJ1009】GT考试(KMP算法,矩阵快速幂,动态规划)

    [BZOJ1009]GT考试(KMP算法,矩阵快速幂,动态规划) 题面 BZOJ 题解 看到这个题目 化简一下题意 长度为\(n\)的,由\(0-9\)组成的字符串中 不含串\(s\)的串的数量有几个 ...

  6. 微信小程序-布局

    flex-direction 传送门 border 传送门 边框 粗细:thin(细线).medium(中粗线)和thick(粗线) 类型:九个确定值:none(无边框线). dotted(由点组成的 ...

  7. PAT乙级-1056. 组合数的和(15)

    给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字.要求所有可能组合出来的2位数字的和.例如给定2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入 ...

  8. gradle build docker image

    前言:其实gradle-docker插件干的事和我们手动制作镜像是一样的,只不过它封装了一些步骤而已. eg:如果我们要将项目打包成镜像,首先我们要写Dockerfile,这是制作镜像的不可或缺的第一 ...

  9. 用vue开发一个app(4,一个久等了的文章)H5直播平台登录注册(1)

    我上一篇关于vue的文章和这一篇时间隔了有点久了.最近终于写完了. 因为我一直想写个有点实绩的东西,而不是随便写一个教程一样东西.结合最近在项目中学到的经验和我的一点创意. 首先介绍下这是个什么! H ...

  10. Linux如此“自私”?

    Linux如此“自私”? “如果当时我真的知道从头建立一个操作系统的难度,肯定是不会有勇气去做的.”1991年8月25日,随着林纳斯·托瓦兹(Linus Torvalds)这句“天真”的描述,Linu ...