17. Letter Combinations of a Phone Number(bfs)
2-9
inclusive, 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. Note that 1 does not map to any letters.
Example:
Input: "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.
class Solution {
public List<String> letterCombinations(String digits) {
LinkedList<String> res = new LinkedList<String>();
if(digits.isEmpty()) return res;
String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
res.add("");
for(int i=0;i<digits.length();i++){
int x=Character.getNumericValue(digits.charAt(i));
while(res.peek().length()==i){
String t = res.remove();
for (char s :mapping[x].toCharArray())
res.add(t+s);
}
}
return res;
}
}
17. Letter Combinations of a Phone Number(bfs)的更多相关文章
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 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所有可能的输出: ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- [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 digit string, return all possible letter combinations that the number could represent. A map ...
随机推荐
- 解决pathForResource返回nil, 无法读取plist文件问题
有很多人在设置plist文件的时候, 会发现读取不了plist文件里面的内容, 返回值为nil, 下面我们来解决一下这个问题. 首先我们打开工程并且按照下面的步骤来设置: 设置好后, 我们来写一段代码 ...
- dp单调性优化
跟着书上的思路学习dp的单调性优化觉得还是很容易想的. 数据范围: dp,数据范围是百万,这应该是O(n)的算法了. 首先不难想到设f[i]表示到第i个百米所能达到的最大能量,那么f[n]即为所求. ...
- 树和二叉树->基础知识
树的表示方法 1 一般表示法 2 广义表表示法 3 凹入表示法 树的基本术语: 树:n(n>=0)个结点的有限集 结点:包含一个数据元素及若干指向其子树的分支 结点的度:结点拥有的子树数成为结点 ...
- TZOJ:最大连续子序列
描述 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K.最大连续子 ...
- tomcat去掉项目名称
进入tomcat的conf目录,查看server.xml,找到<Host></Host>,在里面添加<Context path="" docBase= ...
- java Map 怎么遍历
java中遍历MAP的几种方法 Java代码 Map<String,String> map=new HashMap<String,String>(); map.put(& ...
- ubuntu上u-boot的编译
1,下载u-boot 2,将Windows中的u-boot复制到ubuntu虚拟机中自定义目录并解压 3,进入该目录cd 4,安装dtc:sudo apt-get install device-tre ...
- Linux查看访问IP
Linux查看访问IP https://blog.csdn.net/tojohnonly/article/details/82772323
- loadrunner压测工具
https://www.cnblogs.com/xxyBlogs/p/4579509.html https://www.cnblogs.com/jaazz/p/8668214.html
- [LeetCode] 872. Leaf-Similar Trees_Easy tag: DFS
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form ...