My iterative sollution


  • 0
    Q
    public class Solution {
    String[][] c = { { "a", "b", "c" }, { "d", "e", "f" }, { "g", "h", "i" }, { "j", "k", "l" }, { "m", "n", "o" },
    		{ "p", "q", "r", "s" }, { "t", "u", "v" }, { "w", "x", "y", "z" } };
    public List<String> letterCombinations(String digits) {
    	StringBuilder sb = new StringBuilder(digits);
    	List<String> l = new LinkedList<String>();
    	int s = sb.charAt(0) - '0';
    	for (int i = 0; i < c[s - 2].length; i++) {
    		l.add(c[s - 2][i]);
    	}
    	for (int i = 1; i < sb.length(); i++) {
    		int a = sb.charAt(i) - '0';
    		l = append(a, l);
    	}
    	return l;
    }
    public List<String> append(int n, List<String> l) {
    	List<String> re = new LinkedList<String>();
    	for (int i = 0; i < l.size(); i++) {
    		for (int j = 0; j < c[n - 2].length; j++) {
    			re.add(new StringBuilder(l.get(i)).append(c[n - 2][j]).toString());
    		}
    	}
    	return re;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.