题目链接 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. 如何在Web.config中注册用户控件和自定义控件

    问题: 在ASP.NET 的早先版本里,开发人员通过在页面的顶部添加 指令来引入和使用自定义服务器控件和用户控件时,象这样: <%@ Register TagPrefix="scott ...

  2. Linux学习笔记<五>——<Shell部分>

    管道命令(pipe) 1.把一个命令的输出作为另一个命令的输入 ls -al /etc | less 2.选取命令:cut和grep cut命令可以将一段消息的某段切出来. -d接分隔符,-f是取出第 ...

  3. XcenServer和XcenCenterter的安装

    XcenServer安装 安装在客户端(作为服务器的电脑) 准备工具:U盘(4G以上).uiso9(iso刻录) 步骤一:下载ISO文件, 下载地址:https://xenserver.org/ove ...

  4. 【WP8】让TextBox文本支持滑动(Scroll)

    通过修改样式让TextBox支持文本滑动 在Silverlight上,TextBox是有文本滚动的功能的,当TextBox的文本过长时,可以进行拖动的,TextBox使用 VerticalScroll ...

  5. 各大公司Java面试题超详细总结

    ThreadLocal(线程变量副本)Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量.采用空间换时间,它用于线程间的数据隔离,为每一个使用该变量的线程提供一个副 ...

  6. A股最新的自由现金流和折现估值查询

    A股最新的自由现金流折现估值,利用自由现金流折现的经典公式,采用 8%.9%.10%.11%.12%.15% 等贴现率来进行估值. SH600000:浦发银行的最新自由现金流和折现估值模型: 浦发银行 ...

  7. MTK 预置apk

    一.如何将带源码的APK预置进系统? 1)     在 packages/apps 下面以需要预置的 APK的 名字创建一个新文件夹,以预置一个名为Test的APK 为例 2)     将 Test ...

  8. [MySQL] 01- Basic sql

    准备 一.配置 1. 登录:mysql -u root -p  2. phpMyAdmin创建数据库,并导入.sql文件. 3. 支持中文:set names utf8; 二.面试题 参考:面试宝典- ...

  9. Eclipse------如何将项目通过maven编译并打包

    1.右击项目>>>点击Debug As>>>点击 Maven install进行编译,编译成功后入图 2.右击项目>>>点击Debug As> ...

  10. 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式

    7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...