递归

class Solution {
    List ls;
    String[] arr = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    public List letterCombinations(String digits) {
        ls = new ArrayList();
        ls.clear();
        if (digits != null && digits.length() > 0)
            dfs( digits, "");
        return ls;
    }
    public void dfs( String num, String res) {
        if (num.length() == 0)
            ls.add(res);
        else {
            int flag = num.charAt(0) - '0';
            for (int i = 0; i < arr[flag].length(); i++) {
                dfs( num.substring(1), res + arr[flag].charAt(i));
            }
        }
    }
}

循环

class Solution {
    String[] arr = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    public List letterCombinations(String digits) {
        List ls = new ArrayList();
        ls.clear();
        if (digits != null && digits.length() > 0) {
            ls.add("");
            for(int i = 0 ; i < digits.length() ;i++) {
                List temp = new ArrayList();
                for(int x = 0 ; x < arr[digits.charAt(i)-'0'].length() ;x++) {
                    for(String y : ls) {
                        temp.add(y+arr[digits.charAt(i)-'0'].charAt(x));
                    }
                }
                ls = temp;
            }
        }
        return ls;
    }
}