【LeetCode】17. Letter Combinations of a Phone Number
题目:
思路:设置两个List,一个存储当前层,一个存储最终层
public class Solution {
public List<String> letterCombinations(String digits) {
List<String> listRet=new ArrayList<String>();
if(digits==""){
return listRet;
}
Map<Character,String> map=new HashMap<Character,String>();
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"); for(int i=0;i<digits.length();i++){
List<String> listTem=new ArrayList<String>();
if(i==0){
char[] initials=map.get(digits.charAt(0)).toCharArray();
for(char initial:initials){
listRet.add(String.valueOf(initial));
}
continue;
}
String s1=map.get(digits.charAt(i));
char[] letters=s1.toCharArray();//当前数字对应字符串
for(char letter:letters){
for(String pre:listRet){
listTem.add(pre+String.valueOf(letter));//前缀+当前层字符
}
}
listRet=listTem;
}
return listRet;
}
}
【LeetCode】17. Letter Combinations of a Phone Number的更多相关文章
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
- 【LeetCode】017. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode:17. Letter Combinations of a Phone Number(Medium)
1. 原题链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 题目要求 给定一 ...
- 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 ...
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
随机推荐
- WINDOWS黑客基础(5):利用内存来进行获取计算结果
在前面的注入代码的章节中,我们利用了VirtualAllocEx来在对方的进程开辟了一块内存,并且将代码复制进对方进程的内存里面,从而执行那段内存的代码,但是这里有一个问题,就是代码不是执行在我们进程 ...
- 黄聪:MYSQL5.6缓存性能优化my.ini文件配置方案
使用MYSQL版本:5.6 [client] …… default-character-set=gbk default-storage-engine=MYISAM max_connections=10 ...
- 7. redis优化
一. redis使用上的优化 精简键名和键值 键名:尽量精简,但是也不能单纯为了节约空间而使用不易理解的键名. 键值:对于键值的数量固定的话可以使用0和1这样的数字来表示,(例如:male/femal ...
- (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.
ASP.NET: System.BadImageFormatException: An attempt was made to load a program with an incorrect for ...
- java: org.luaj.vm2.LuaError:XXX module not found lua脚本初始化出错
我遇到这个错误是因为在引用脚本目录时,设置错了位置.设置成脚本所在目录的上级目录. lua使用和加载初始化方法 在java中使用lua,使用需要引用 luaj-jse-2.0.2.jar 同时需要使用 ...
- LinkedHashMap的实现原理(复习)
1. LinkedHashMap概述: LinkedHashMap是Map接口的哈希表和链接列表实现,具有可预知的迭代顺序.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映 ...
- BEvent_客制化Event Agent通道(案例)(待整理)
2014-09-09 Created By BaoXinjian
- hdu 1561 The more, The Better 背包型树形DP 简单题
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- c# 函数及out传值
使用 out传值的时候仅仅是将变量名(箱子)拿过来,并不会管之前是什么值函数体结束之前必须对该out的参数进行赋值,否则报错(不好意思还回去)out传值,可以进行多个值的传回 public void ...
- JAVA 数组实例-求学生平均成绩,与计算数组的长度
实例: 知识点:数组名.length是计算数组的长度 import java.util.*; //求学生平均分成绩 public class Test{ public static void main ...