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

相关文章推荐

17. Letter Combinations of a Phone Number

此道题目为经典的DFS题目,可以用DFS解决。很长时间没有写递归题目了,所以我的解法占用空间较大,比较高效的写法是只在栈中维护一个字符序列,而我的解法则是维护了3^N个字符序列,其中N为输入的digi...

【Leetcode】17. Letter Combinations of a Phone Number

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

[Leetcode]17. Letter Combinations of a Phone Number

题目大意:点击打开链接 Given a digit string, return all possible letter combinations that the number could re...

LeetCode 17. Letter Combinations of a Phone Number 递归回溯法

17. Letter Combinations of a Phone Number Given a digit string, return all possible letter combina...

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

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

LeetCode之17_Letter Combinations of a Phone Number

题目原文: Given a digit string, return all possible letter combinations that the number cou...

LeetCode 17 Letter Combinations of a Phone Number 数字转为字母组合

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

leetcode17:Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Given a digit string, return all possible letter combinatio...
  • GODBAR
  • GODBAR
  • 2016年12月07日 08:06
  • 170

Leetcode--17:Letter Combinations of a Phone Number

Letter Combinations of a Phone Number
  • Caipta
  • Caipta
  • 2017年07月24日 22:43
  • 97

LeetCode 17_Letter Combinations of a Phone Number

快中秋节了,心情不佳,多写几道吧,只有编程能带给我片刻的宁静啊。。。 今天给大家讲一下第17题,难度为中等,这个题目考察的到不是什么高深的算法,考的是对编程技巧的掌握吧,对语言应用方面要求多余算法设...
  • cyfcsd
  • cyfcsd
  • 2015年09月25日 10:59
  • 395
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目72:17. Letter Combinations of a Phone Number
举报原因:
原因补充:

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