LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description
HashMap<Character, String> map = new HashMap<>();
map.put('', "");
map.put('', "");
map.put('', "abc");
map.put('', "def");
map.put('', "ghi");
map.put('', "jkl");
map.put('', "mno");
map.put('', "pqrs");
map.put('', "tuv");
map.put('', "wxyz");
My java solution with FIFO queue
public List<String> letterCombinations(String digits) {
LinkedList<String> ans = new LinkedList<String>();
String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ans.add("");
for(int i =0; i<digits.length();i++){
int x = Character.getNumericValue(digits.charAt(i));
while(ans.peek().length()==i){
String t = ans.remove();
for(char s : mapping[x].toCharArray())
ans.add(t+s);
}
}
return ans;
}
参考代码:
package leetcode_50; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; /***
*
* @author pengfei_zheng
* 优先队列使用
*/
public class Solution17 {
public List<String> letterCombinations(String digits) { LinkedList<String> ans = new LinkedList<String>();
if(digits == null || digits.length() == 0){
return ans;
}
HashMap<Character, String> map = new HashMap<>();
map.put('0', "0");
map.put('1', "1");
map.put('2', "abc");
map.put('3', "def");
map.put('4', "ghi");
map.put('5', "jkl");
map.put('6', "mno");
map.put('7', "pqrs");
map.put('8', "tuv");
map.put('9', "wxyz");
ans.add("");
for(int i =0; i<digits.length();i++){
char key = digits.charAt(i);
while(ans.peek().length()==i){//遍历直到达到电话号码长度时结束
String t = ans.remove();//移除旧的ans值
for(char s : map.get(key).toCharArray())
ans.add(t+s);//添加新的ans
}
}
return ans;
}
}
LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)的更多相关文章
- [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电话号码的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- 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
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- 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 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 ☆☆
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode——17. Letter Combinations of a Phone Number
一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
随机推荐
- 大爱HTML5 9款超炫HTML5最新动画源码
我们分享过很多漂亮的HTML5动画,包括CSS3菜单.HTML5 Canvas动画等.今天我们精选了9款非常不错的超炫HTML5最新动画及其源码,一起来看看. 1.HTML5可爱的404页面动画 很逗 ...
- VS2010 工程设置
本篇文章的主要内容转载自 http://blog.csdn.net/waitforfree/article/details/8622059 ,感谢博主的辛苦劳动.此处,对比较重要的部分,进行进一 ...
- MFC中CString.Format的详细用法
本文转载自:http://blog.csdn.net/wangkaishou/article/details/5846152,感谢网友 Technorati 标签: CString For ...
- 安卓开发笔记——关于Handler的一些总结(上)
接上篇文章<安卓开发笔记——关于AsyncTask的使用>,今天来讲下在安卓开发里"重中之重"的另一个异步操作类Handler. 今天打算先讲下关于Handler的一些 ...
- Thinkphp5 Route用法
域名路由:domain 1.application/router.php 文件位置,吧一下代码放进去就可以了 use think\Route; Route::domain('app.tp5a.com' ...
- Objective-C 语法之 NSURL
有时我们需要获取请求地址的相关信息,这时我们就可以用 NSURL 的一些方法操作来获取它. 需要注意的一点是:请求地址里可能存在特殊字符或中文,为了正确获取信息,建议使用 stringByAdding ...
- UNIX环境编程学习笔记(18)——进程管理之进程控制三部曲
lienhua342014-10-05 1 进程控制三部曲概述 UNIX 系统提供了 fork.exec.exit 和 wait 等基本的进程控制原语.通过这些进程控制原语,我们即可完成对进程创建.执 ...
- 二维码解析:使用 JavaScript 库reqrcode.js解析二维码
上次使用QRCode.js可以来生成二维码,但是我没有找到有文档说明可以对存在的二维码进行扫描解析其中的内容. 幸亏查找到了可行的解决方案,而且很好使哦!就是reqrcode.js 地址:https: ...
- repo_folder
-- Create table create table REPO_FOLDER ( UUID ) not null, VALID ) not null, CREATE_TIME ) not null ...
- Java -- 异常的捕获及处理 -- 范例 -- throw与throws的应用
7.2.3 范例 -- throw与throws的应用 例:综合应用 Class : Math package limeThrowable._7_2_3; public class Math { pu ...