[LeetCode]Letter Combinations of a Phone Number

public class Solution {
    List<String> result = new ArrayList<String>();
    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0) {
            return result;
        }
        String[] strs = "abc def ghi jkl mno pqrs tuv wxyz".split(" ");
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < digits.length(); i++) {
            list.add(strs[(int)digits.charAt(i) - '0' - 2]);
        }
        helper(list, "");
        return result;
    }
    public void helper(List<String> list, String pre) {
        int length = list.size();
        if (length == 0) {
            result.add(pre);
            return;
        }
        String str = list.get(0);
        list.remove(0);
        for (int i = 0; i < str.length(); i++) {
            String tmp = pre + str.charAt(i);
            helper(list, tmp);
        }
        list.add(0, str);
    }
}

 

posted @ 2015-12-03 10:13  Weizheng_Love_Coding  阅读(109)  评论(0编辑  收藏  举报