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. Error #2044: 未处理的 ioError:。 text=Error #2032

    1.错误描述 Error #2044: 未处理的 ioError:. text=Error #2032: 流错误. URL: http://127.0.0.1:8080/HBMB/analysis/a ...

  2. MongoDB添加用户验证

    Mongodb默认启动是不带认证,也没有账号,只要能连接上服务就可以对数据库进行各种操作,这样可不行.现在,我们得一步步开启使用用户和认证. 第一步,我们得定位到mongodb的安装目录.我本机的是C ...

  3. 关于spring事务注解实战

    1.概述 spring的事务注解@Transaction 相信很多人都用过,而@Transaction 默认配置适合80%的配置. 本篇文章不是对spring注解事务做详细介绍,而是解决一些实际场景下 ...

  4. CSS实现网页背景图片自适应全屏

    一张清晰漂亮的背景图片能给网页加分不少,设计师也经常会给页面的背景使用大图,我们既不想图片因为不同分辨率图片变形,也不希望当在大屏的情况下,背景有一块露白,简而言之,就是实现能自适应屏幕大小又不会变形 ...

  5. Linux性能分析工具top命令详解

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,常用于服务端性能分析. top命令说明 [www.linuxidc.com@linuxidc-t-tomcat-1 ...

  6. 【Luogu1373】小a和uim之大逃离(动态规划)

    [Luogu1373]小a和uim之大逃离(动态规划) 题面 题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电,一阵阵雷声.刹那间,狂风大作,乌云布 ...

  7. [HNOI2013]游走

    题面在这里 题意 从1号点开始等概率选择路径并加上边权,直到到达n号点结束,要求将m条边赋权值1-m使得期望最小 sol 续上文 zsy ycb orz 简单的贪心:求出每条边的期望经过次数,sort ...

  8. 论文笔记(6):Weakly-and Semi-Supervised Learning of a Deep Convolutional Network for Semantic Image Segmentation

    这篇文章的主要贡献点在于: 1.实验证明仅仅利用图像整体的弱标签很难训练出很好的分割模型: 2.可以利用bounding box来进行训练,并且得到了较好的结果,这样可以代替用pixel-level训 ...

  9. xx-net连接教程

    第一步:安装xx-net 在github上下载xx-net,网址 解压后点击运行start.bat文件,此时会提醒是不信任的文件,此时在系统偏好设置里的安全性与隐私去设置让它能打开. 第二步:安装Sw ...

  10. LVS-NAT模式的配置详解

    由于实验室拟态存储的项目需要通过NAT模式来映射NFS服务器已实现负载均衡的目的,通过调研了多种负载均衡机制,笔者最终选择了LVS的NAT模式来实现需求,接下来通过博客来记录一下LVS-NAT模式的配 ...