Given a string containing digits from 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.

这题要求列出所有可能古德字符组合,我们先把1-9的字符列出来,然后从digits中一次取出数字代表的字符,然后做笛卡尔积的处理

这里的难点在于2个数的时候好处理,2层的循环就能解决,但是3个数,4个数怎么做呢?
4个数时,我们需要先求出2个数的笛卡尔积,然后再去处理第3个,第4个

所以我们在2层循环上,加上一层控制,在外部选定完i后,我们要在原来的字符串基础上加上第i个数字代表的字符,我们每次把list(0)的元素remove出来,然后加上

新的字符再添加到list的末尾,这样我们就可以根据list(0)的长度判断这一轮字符有没有全部加上,就像[b,c,ad,ae,af]这样,此时i=1,list.get(0).length()=1,这就说明还有这轮还没加完

直到[ad, ae, af, bd, be, bf, cd, ce, cf],此时i=1但list.get(0).length()=2,说明这轮结束,回到外层

class Solution {
public List<String> letterCombinations(String digits) {
List<String> list = new ArrayList<String>();
if(digits.isEmpty()) return list;
String[] str = new String[] {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
list.add("");
String temp = null;
for(int i = 0; i < digits.length(); i++) {
int k = Integer.parseInt(digits.charAt(i)+"");
while(list.get(0).length() == i)
{
temp = list.remove(0);
for(int j = 0; j < str[k].length(); j++){
list.add(temp + str[k].charAt(j));
}
}
}
return list;
}
}

[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 电话号码的字母组合

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

  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. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

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

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

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

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

随机推荐

  1. [Django笔记] admin 深入学习

    admin django 内置的管理后台,大部分时候可以通过对admin进行配置来提高开发效率. 数据列表展示 默认情况下显示一个models-objects的列表,如果model定义了 __str_ ...

  2. winform 动态添加控件及事件

    for (int i = 0; i < 4; i++) { Button btn = new Button(); //btn.Name = dt.Rows[i]["ANDON_CONT ...

  3. CF70D(动态凸包)

    CF70D(动态凸包) 给出q(<=1e5)个询问,每次在加上一个点,维护凸包,或者询问某个点是否在凸包内(在边上也算). 听说可以用cdq做--但是并不会.我等蒟蒻只会用平衡树做. 首先,假设 ...

  4. Task4

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. CompressFormat压缩性能

    在产品应用场景内有个需求,Bitmap原图压缩保存.但是每次保存一个图片,需要500+ms.原本以为是sd卡写的慢.后来测试发现是图片压缩问题.试验过CompressFormat PNG和JPEG两种 ...

  6. 明明有印象却找不到,APP内搜索为什么这么难用?

    赶上了互联网浪潮的当代人,每当有任何困扰,第一反应都是打开搜索引擎. 什么叫做“硬核相亲”,什么是“pick一下”,“达达主义”,“隐形贫困人口”——你都默默搜索过,不想被时代与话题抛弃.也许只有这样 ...

  7. Eclipse报错:Attribute "xmlns" was already specified for element "web-app".

    原因: 在Eclipse中修改了已有的Web工程的工程名,然后出现该错误. Attribute "xmlns" was already specified for element ...

  8. C语言中函数声明、形参、实参

    函数原型: 原型prototype是函数的声明:描述了函数的返回值与参数: 函数原型说明了两点: 1.该函数的返回值 2.该函数的参数及其类型 ++++++++++++++++++++++++++++ ...

  9. Ubuntu server 修改系统时区

    执行命令: sudo dpkg-reconfigure tzdata

  10. HDU5952 Counting Cliques计算完全图的个数 巧妙构图+dfs

    题目传送门 题目大意:给出n个点,m条无向边,让你计算这幅母图中有几个大小为s的完全图. 完全图的意思是任意一个点都和其他点直接相连,完全图的大小指的就是完全图点的个数. 思路:比较巧妙的构图方式.我 ...