[LeetCode][Java] Letter Combinations of a Phone Number

原创 2015年07月09日 19:03:39

题目:

Given a digit string, 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.


Input:Digit string "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.

题意:

给定一个数字字符串,返回这个数字能代表的所有的字母的组合。

数字对字母的映射如下图所示(就在电话的按键中)

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
返回的结果可以按照任意的顺序。

算法分析:

 * 思路:


 *比如“234”这个字符串,我可以先将0...1的所有排列找到-->{"a", "b", "c"}


 *再进一步将0...2的所有排列找到-->{"ad", "ae","af", "bd", "be", "bf", "cd", "ce", "cf"}


 *如此循环...直到字符串末尾。实现如下

AC代码:

public class Solution 
{
    public  ArrayList<String> letterCombinations(String digits) 
    {
    	ArrayList<String> res=new ArrayList<String>();
    	String charmap[] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    	if (digits.length()==0) return res;
    	res.add("");
        for (int i = 0; i < digits.length(); i++)
        {
          ArrayList<String> tempres=new ArrayList<String>(); 
          String chars = charmap[digits.charAt(i) - '0'];
          for (int c = 0; c < chars.length();c++)
          {
            for (int j = 0; j < res.size();j++)
              tempres.add(res.get(j)+chars.charAt(c));
          }
            res = tempres;
        }
        return res;
    }
}


版权声明:本文为博主原创文章,转载注明出处

相关文章推荐

[LeetCode] 017. Letter Combinations of a Phone Number (Medium) (C++/Java/Python)

[LeetCode] 017. Letter Combinations of a Phone Number (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-03-04 15:47
  • 2613

[Java]LeetCode17 Letter Combinations of a Phone Number

[Java]LeetCode17 Letter Combinations of a Phone Number

LeetCode 17 Letter Combinations of a Phone Number(C,C++,Java,Python)

Problem: Given a digit string, return all possible letter combinations that the number could re...

【LeetCode-面试算法经典-Java实现】【017-Letter Combinations of a Phone Number (电话号码上的单词组合)】

【017-Letter Combinations of a Phone Number (电话号码上的单词组合)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given ...

Letter Combinations of a Phone Number [leetcode]谈谈循环解法的两种思路

本系列博文中有很多两种思路的,其实是因为第一遍刷题的时候有一个想法,第二遍刷题的时候已经忘掉之前的思路了,又有新的想法了。 同时大部分代码我也同时PO到leetcode的对应题目的问答中去了,所以如...

Letter Combinations of a Phone Number - Leetcode

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

[leetcode 18] Letter Combinations of a Phone Number

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

LeetCode Letter Combinations of a Phone Number

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

[Leetcode]Letter Combinations of a Phone Number

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

LeetCode:Letter Combinations of a Phone Number

这题没什么算法,就是锻炼编程能力,是很好的一个题目。 import java.util.ArrayList; import java.util.Arrays; import java.uti...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)