17. Letter Combinations of a Phone Number

class Solution {
    public List letterCombinations(String digits) {
        Map phone = new HashMap();
        phone.put('2', "abc");
        phone.put('3', "def");
        phone.put('4', "ghi");
        phone.put('5', "jkl");
        phone.put('6', "mno");
        phone.put('7', "pqrs");
        phone.put('8', "tuv");
        phone.put('9', "wxyz");
        
        List output = new ArrayList();
        
        for(int i = 0; i < digits.length(); i++) {
            String letter = phone.get(digits.charAt(i));
            List temp = new ArrayList();
            for(int j = 0; j < letter.length(); j++) {
                if(output.size() == 0) {
                    temp.add(letter.substring(j, j+1));
                }
                else {
                    for(String s : output) {
                        temp.add(s+letter.substring(j, j+1));
                    }
                }
            }
            output.clear();
            output.addAll(temp);
            temp.clear();
        }
        return output;
    }
}

你可能感兴趣的:(leetcode,java)