码迷,mamicode.com
首页 > 其他好文 > 详细

Letter Combinations of a Phone Number

时间:2015-02-13 18:20:16      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/

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.

解题思路:

针对输入的字符串digits,生成一个二维数组,横列是digits的每个字符,数列就是每个字符可能代表的字母。这里要注意,7和9可能代表4个字母,其他数字可能代表3个字母。

下面的问题就变成,每个字符选出一个字母,可能有多少种情况。比较典型的可以用dfs求解的问题。

public class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> returnList = new ArrayList<String>();
        if(digits.length() == 0){
            returnList.add("");
            return returnList;
        }
        
        char[][] chars = new char[digits.length()][4];
        for(int i = 0; i < digits.length(); i++){
            if(digits.charAt(i) >= ‘2‘ && digits.charAt(i) <= ‘6‘){
                for(int j = 0; j < 3; j++){
                    // if(j == 3){
                    //     chars[i][j] = ‘1‘;
                    // }
                    chars[i][j] = (char)(‘a‘ + (digits.charAt(i) - ‘0‘ - 2) * 3 + j);
                }
            }
            if(digits.charAt(i) >= ‘7‘ && digits.charAt(i) <= ‘9‘){
                if(digits.charAt(i) == ‘7‘){
                    chars[i] = new char[]{‘p‘,‘q‘,‘r‘,‘s‘};
                }
                if(digits.charAt(i) == ‘8‘){
                    chars[i] = new char[]{‘t‘,‘u‘,‘v‘};
                }
                if(digits.charAt(i) == ‘9‘){
                    chars[i] = new char[]{‘w‘,‘x‘,‘y‘,‘z‘};
                }
            }
        }
        
        StringBuffer bf = new StringBuffer();
        dfs(returnList, chars, 0, bf);
        return returnList;
    }
    
    public static void dfs(List<String> returnList, char[][] chars, int start, StringBuffer bf){
        if(start == chars.length){
            returnList.add(bf.toString());
            return;
        }
        for(int i = 0; i < chars[start].length; i++){
            if(chars[start][i] == ‘\0‘){
                return;
            }
            bf.append(chars[start][i]);
            dfs(returnList, chars, start + 1, bf);
            bf.deleteCharAt(start);
        }
    }
}

这里的dfs递归实际上只用到一个变量start,所以也可以将其他变量声明为成员变量。

public class Solution {
    List<String> returnList;
    char[][] chars;
    StringBuffer bf = new StringBuffer();

    public List<String> letterCombinations(String digits) {
        returnList = new ArrayList<String>();
        if(digits.length() == 0){
            returnList.add("");
            return returnList;
        }
        
        chars = new char[digits.length()][4];
        for(int i = 0; i < digits.length(); i++){
            if(digits.charAt(i) >= ‘2‘ && digits.charAt(i) <= ‘6‘){
                for(int j = 0; j < 3; j++){
                    // if(j == 3){
                    //     chars[i][j] = ‘1‘;
                    // }
                    chars[i][j] = (char)(‘a‘ + (digits.charAt(i) - ‘0‘ - 2) * 3 + j);
                }
            }
            if(digits.charAt(i) >= ‘7‘ && digits.charAt(i) <= ‘9‘){
                if(digits.charAt(i) == ‘7‘){
                    chars[i] = new char[]{‘p‘,‘q‘,‘r‘,‘s‘};
                }
                if(digits.charAt(i) == ‘8‘){
                    chars[i] = new char[]{‘t‘,‘u‘,‘v‘};
                }
                if(digits.charAt(i) == ‘9‘){
                    chars[i] = new char[]{‘w‘,‘x‘,‘y‘,‘z‘};
                }
            }
        }
        
        dfs(0);
        return returnList;
    }
    
    public void dfs(int start){
        if(start == chars.length){
            returnList.add(bf.toString());
            return;
        }
        for(int i = 0; i < chars[start].length; i++){
            if(chars[start][i] == ‘\0‘){
                return;
            }
            bf.append(chars[start][i]);
            dfs(start + 1);
            bf.deleteCharAt(start);
        }
    }
}

 

Letter Combinations of a Phone Number

标签:

原文地址:http://www.cnblogs.com/NickyYe/p/4290485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!