Middle-题目72:17. Letter Combinations of a Phone Number

原创 2016年05月31日 16:51:56

题目原文:
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”].
题目大意:
给出一个九键键盘,和数字序列,求出所有可能的对应字母组合。
题目分析:
还是一个暴力的回溯法,使用backtrack(List list, String digits, String currentLetter, int index)维护搜索过程,其中list是字母组合的列表,digits是数字序列,currentLetter是当前的搜索结果,index记录digits串中该搜索的位置。
源码:(language:java)

public class Solution {
    private String[][] letters = new String[][] {
        {" "},
        {},
        {"a","b","c"},
        {"d","e","f"},
        {"g","h","i"},
        {"j","k","l"},
        {"m","n","o"},
        {"p","q","r","s"},
        {"t","u","v"},
        {"w","x","y","z"}
    };
    public List<String> letterCombinations(String digits) {
        List<String> list = new ArrayList<String>();
        if(digits.length() > 0)
            backtrack(list, digits, new String(), 0);
        return list;
    }
    private void backtrack(List<String> list, String digits, String currentLetter, int index) {
        if(index == digits.length()) {
            list.add(currentLetter);
            return;
        }
        else {
            int num = digits.charAt(index) -'0';
            for(int i = 0; i < letters[num].length; i++) {
                currentLetter+=letters[num][i];
                backtrack(list, digits, currentLetter, index+1);
                currentLetter = currentLetter.substring(0,currentLetter.length()-1);
            }
        }
    }
}

成绩:
1ms,beats 46.02%,众数1ms,53.98%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

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...
  • runningtortoises
  • runningtortoises
  • 2015年05月10日 13:39
  • 2316

[Java]LeetCode17 Letter Combinations of a Phone Number

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

17. Letter Combinations of a Phone Number-python

Leetcode上对deque不支持,所以用list和游标模拟队列,应为这个题目是队列的经典应用。原题Given a digit string, return all possible letter ...
  • daigualu
  • daigualu
  • 2017年07月02日 10:03
  • 577

[leetcode] 17. Letter Combinations of a Phone Number ,python实现【medium】

Letter Combinations of a Phone Number Given a digit string, return all possible letter comb...
  • zl87758539
  • zl87758539
  • 2016年06月14日 22:25
  • 786

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

题目链接:Letter Combinations of a Phone Number Given a digit string, return all possible letter combina...
  • makuiyu
  • makuiyu
  • 2015年01月30日 21:30
  • 2541

【LeetCode】LeetCode——第17题:Letter Combinations of a Phone Number

【LeetCode】LeetCode——第17题:Letter Combinations of a Phone Number
  • hujingshuang
  • hujingshuang
  • 2016年04月29日 21:40
  • 4668

[leetcode-17]Letter Combinations of a Phone Number(java)

问题描述: Given a digit string, return all possible letter combinations that the number could represent...
  • zdavb
  • zdavb
  • 2015年07月27日 14:29
  • 427

【LeetCode】C# 17、Letter Combinations of a Phone Number

类似九宫格输入法,输入两个数字之后返回所有可能的字母组合。
  • qq_39643935
  • qq_39643935
  • 2017年10月09日 12:12
  • 61

LeetCode #17 Letter Combinations of a Phone Number C# Solution

Given a digit string, return all possible letter combinations that the number could represent. e...
  • daypig
  • daypig
  • 2016年06月01日 21:19
  • 365

LeetCodet题解--17. Letter Combinations of a Phone Number(所有数字键盘组合所对应的所有字符集合)

链接 LeetCode题目:https://leetcode.com/problems/letter-combinations-of-a-phone-number/ GitHub代码:h...
  • gatieme
  • gatieme
  • 2016年04月07日 16:53
  • 1586
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目72:17. Letter Combinations of a Phone Number
举报原因:
原因补充:

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