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

LeetCode 17: Letters Combination of a Phone Number

时间:2017-09-10 09:58:48      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:final   res   build   string   div   generate   void   int   ati   

class Solution {
    private final String[] map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if (digits.length() == 0) {
            return result;
        }
        generateComb(digits.toCharArray(), result, new StringBuilder(), 0);
        return result;
    }
    
    private void generateComb(char[] digits, List<String> result, StringBuilder current, int index) {
        if (index == digits.length) {
            result.add(current.toString());
            return;
        }

        int pos = (int)(digits[index] - ‘0‘);
        
        for (int i = 0; i < map[pos].length(); i++) {
            current.append(map[pos].charAt(i));
            generateComb(digits, result, current, index + 1);
            current.setLength(current.length() - 1);
        }
    } 
}

 

LeetCode 17: Letters Combination of a Phone Number

标签:final   res   build   string   div   generate   void   int   ati   

原文地址:http://www.cnblogs.com/shuashuashua/p/7500081.html

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