[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
  • 2731

[Java]LeetCode17 Letter Combinations of a Phone Number

[Java]LeetCode17 Letter Combinations of a Phone Number
  • fumier
  • fumier
  • 2015年04月24日 09:47
  • 912

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

LeetCode Letter Combinations of a Phone Number 经典组合问题

这个问题主要是知道如何取字母出来和其他字母串组合: 包含了三个循环: 第一个循环:取所有“字母串”(指2,3代表的abc,def)出来,逐个组合; 第二个循环:取之前已经组合好的“所有结果组合”...

leetcode-17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A ...
  • qj30212
  • qj30212
  • 2016年09月09日 17:43
  • 204

【LeetCode-17】Letter Combinations of a Phone Number

这道题运用的是dfs的思想,一个小小的错误害得我找了1个小时,以后++i 和 i++一定搞清楚,平时很清楚,用起来咋就不顶事了呢,上代码吧。 递归的时候我把current++ 传进去了,结果总是报栈...

LeetCode 017. Letter Combinations of a Phone Number

Letter Combinations of a Phone Number  Total Accepted: 12594 Total Submissions: 48534My Submissions...

[LeetCode]--17. Letter Combinations of a Phone Number

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

leetcode系列(37)Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Given a digit string,return all possible letter combinations ...
  • macchan
  • macchan
  • 2015年10月19日 22:48
  • 286
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[LeetCode][Java] Letter Combinations of a Phone Number
举报原因:
原因补充:

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