题目链接 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 (电话号码字符组合)的更多相关文章

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

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

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

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

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

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

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

  5. 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...

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

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

  7. Leetcode 17.——Letter Combinations of a Phone Number

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

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

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

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

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

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

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

随机推荐

  1. (个人)Linux基本指令收集

    1. 删除文件 其中 -r为向下递归删除    -f为强行删除,不做提示 rm -rf name 1   1 rm -rf name 2. 目录跳转指令 cd .. --跳转到上一级 cd ../ - ...

  2. linux 内存分页

    内存是计算机的主存储器.内存为进程开辟出进程空间,让进程在其中保存数据.我将从内存的物理特性出发,深入到内存管理的细节,特别是了解虚拟内存和内存分页的概念. 内存 简单地说,内存就是一个数据货架.内存 ...

  3. winform c#中子窗体关闭刷新父窗体

    父窗体Form1 子窗体Form2 Form1中有一个datagridview控件和一添加按钮,Form2中有一个Text控件和一个保存按钮 要求点击Form1窗体上的添加按钮,弹出Form2,再te ...

  4. .net framework 4.0 在 VS2010 安装目录下位置 dotNetFx40_Full_x86_x64.exe在磁盘哪个目录?

    .net framework 4.0 在 VS2010 安装目录下位置 dotNetFx40_Full_x86_x64.exe在磁盘哪个目录? 使用VS2010开发应用程序完毕后,在发布应用程序时,常 ...

  5. GCT之数学公式(三角函数)

  6. C语言的基本数据类型长度

    PS:以下内容是在Xcode的编辑器64位环境下的测试结果,网上有关于64位和32位各数据类型存在的差异,请自行online search. main.m #import <Foundation ...

  7. web_submit_data详解

    定义:处理无状态或者上下文无关的表单提交.它用来生成表单的GET或POST请求,这些请求与Form自动生成的请求是一样的,发送这些请求时不需要表单上下文. 函数形式:web_submit_data( ...

  8. WebGL Matrix4(4*4矩阵库)

    Matrix4是由<<WebGL编程指南>>作者写的提供WebGL的4*4矩阵操作的方法库,简化我们编写的代码.源代码共享地址,点击链接:Matrix4源代码. 下面罗列了Ma ...

  9. 快速排查SQL服务器阻塞语句

    SELECT*FROM sys.sysprocesses and blocked> --可以查看阻塞 SELECT SPID=p.spid, DBName =convert(CHAR(),d.n ...

  10. 输入控件tagsinput

    ​摘要: ​tagsinput是一款基于jQuery的插件.具有组织输入内容.校验.backspace删除等功能.当你在输入框输入结束按下enter键,tagsinput会将你输入的内容用标签封装,每 ...