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. c++标准库介绍

    C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能.<cname>形式的标准头文件[<complex>例外]其内容 ...

  2. 【bzoj1965】: [Ahoi2005]SHUFFLE 洗牌 数论-快速幂-扩展欧几里得

    [bzoj1965]: [Ahoi2005]SHUFFLE 洗牌 观察发现第x张牌 当x<=n/2 x=2x 当x>n/2 x=2x-n-1 好像就是 x=2x mod (n+1)  就好 ...

  3. P1891 疯狂LCM

    \(\color{#0066ff}{ 题目描述 }\) 众所周知,czmppppp是数学大神犇.一天,他给众蒟蒻们出了一道数论题,蒟蒻们都惊呆了... 给定正整数N,求LCM(1,N)+LCM(2,N ...

  4. Maven入门(二)pom.xml和核心概念

    一.pom.xml文件说明 1.pom意思就是project object model. 2.pom.xml包含了项目构建的信息,包括项目的信息.项目的依赖等. 3.pom.xml文件是可以继承的,大 ...

  5. 解决maven项目中有小红叉的问题

    首先在window--perferences--showview中显示problems中查看出错的原因

  6. ubuntu下中文乱码解决

    这个方法只对该用户有效. 方法二:修改/etc/environment,增加以下内容: LANGUAGE=”zh_CN:zh:en_US:en” LANG=zh_CN.GBK

  7. 【Leetcode】Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  8. Visual Studio 2017 Key激活码

     Microsoft Visual Studio Enterprise 2017 企业版 KEY:NJVYC-BMHX2-G77MM-4XJMR-6Q8QFMicrosoft Visual Studi ...

  9. 《C程序设计II》简易计算器,杨辉,数字杯子图形

    <C程序设计II>简易计算器,杨辉,数字杯子图形.<C程序设计II>简易计算器,杨辉,数字杯子图形.<C程序设计II>简易计算器,杨辉,数字杯子图形. <C程 ...

  10. python 中文路径问题

    Python直接读取中文路径的文件时失败,可做如下处理: inpath = 'D:/work/yuanxx/在线导航/驾车导航/walk_log/20130619_172355.txt' uipath ...