Leetcode 17.——Letter Combinations of a Phone Number
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的更多相关文章
- 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 ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- [LeetCode] 17. Letter Combinations of a Phone Number ☆☆
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- LeetCode——17. Letter Combinations of a Phone Number
一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...
随机推荐
- 初识 systemd
从 init 系统说起 linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 bootloader 载入内核,进行内核初始化.内核初始化的最后一步就是启动 PID ...
- filter()和find()的区别
<div class="css"> <p class="rain">测试1</p> </div> <div ...
- C#图解教程 第十章 结构
结构 什么是结构结构是值类型对结构赋值构造函数和析构函数 实例构造函数静态构造函数构造函数和析构函数小结 字段初始化语句是不允许的结构是密封的装箱和拆箱结构作为返回值和参数 关于结构的其他信息 结构 ...
- 消息中间件kafka+zookeeper集群部署、测试与应用
业务系统中,通常会遇到这些场景:A系统向B系统主动推送一个处理请求:A系统向B系统发送一个业务处理请求,因为某些原因(断电.宕机..),B业务系统挂机了,A系统发起的请求处理失败:前端应用并发量过大, ...
- Centos samba 服务配置
1背景 转到Linux有段时间了,vim操作还不能应对工程代码,之前一直都是Gnome桌面 + Clion 作开发环境,无奈在服务器上没有这样的环境, 看同事是(Windows)Source Insi ...
- ETL总结(扫盲版)
1.ETL名词解释 英文缩写 Extract-Transform-Load ,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至到目的端(一般指的是数 ...
- ubuntu下cmake 使用clang
安装llvm.clang sudo apt-get install llvm clang clang命令会在/usr/bin/clang cmake配置交叉编译链 建立linux.toolchain. ...
- 网站压力测试ab 命令
网站压力测试ab 命令 author: headsen chen 2017-10-25 10:06:35 个人原创,转载请注明作者,出处,否则依法追究法律责任! 1,制作一个a ...
- VISUALSVN: UNABLE TO CONNECT TO A REPOSITORY AT URL 无法连接主机的解决办法
场景:我的系统是win7,安装的 VisualSVN Server 作为svn 服务器,昨天是好的,我手渐,使用鲁大师优化了系统,今天提交,更新的时候,直接提示:Unable to connect t ...
- 分享基于Qt5开发的一款故障波形模拟软件
背景介绍 这是一款采用Qt5编写的用于生成故障模拟波形的软件.生成的波形数据用于下发到终端机器生成对应的故障类型,用于培训相关设备维护人员的故障排查技能.因此,在这款软件中实现了故障方案管理.故障波形 ...