Letter Combinations of a Phone Number(电话号码的字母组合)

http://www.lintcode.com/en/problem/letter-combinations-of-a-phone-number/?rand=true#

import java.util.ArrayList;
import java.util.List;

public class Solution {
    /*
     * @param digits: A digital string
     * @return: all posible letter combinations
     */
    public List letterCombinations(String digits) {
        // write your code here
        String phone[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        List list = new ArrayList<>();
        tree(digits, phone, list, "");
        return list;
    }

    private void tree(String digits, String[] phone, List list, String s) {
        if (digits.isEmpty()) {
            if (!s.isEmpty()) {
                list.add(s);
            }
            return;
        }
        int i = digits.charAt(0) - '0';
        for (char c : phone[i].toCharArray()) {
            tree(digits.substring(1), phone, list, s + c);
        }

    }
}

你可能感兴趣的:(Letter Combinations of a Phone Number(电话号码的字母组合))