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

 题目:Letter Combinations of a Phone Number Given a digit string, return all possible letter combi...

[Java]LeetCode17 Letter Combinations of a Phone Number

[Java]LeetCode17 Letter Combinations of a Phone Number

LeetCode OJ-17-Letter Combinations of a Phone Number

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

LeetCode OJ-17.Letter Combinations of a Phone Number(DFS)

LeetCode OJ-17.Letter Combinations of a Phone Number(DFS解法)题目描述 Given a digit string, return all p...

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-17.Letter Combinations of a Phone Number

https://leetcode.com/problems/letter-combinations-of-a-phone-number/ Given a digit string, retu...

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 17 Letter Combinations of a Phone Number(电话号码的字母组合)

翻译给定一个数字字符串,返回所有这些数字可以表示的字母组合。一个数字到字母的映射(就像电话按钮)如下图所示。输入:数字字符串“23” 输出:["ad", "ae", "af", "bd", "be",...
  • NoMasp
  • NoMasp
  • 2015-10-18 10:59
  • 3171

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

题意 给定一个数字字符串,枚举各个数字对应的字母组合后的字符串(不重复)。 题解 一个串中字母的重复次数,即后面字母形成串的可能种数。 给定数字”234”,对应串“abc”、“def”、...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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