232 ms Java Solution


  • -1
    S

    Looking at the graph, this was one of the fastest java solutions so I'm posting it.

    public class Solution {
        public List<String> letterCombinations(String digits) {
            List<String> result = new ArrayList<String>();
            if (digits.length()==0) return result;
            String[] l=new String[] { " "," ","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
            char[] c = digits.toCharArray();
            int[] n = new int[c.length];
            while(true) {
                StringBuilder b=new StringBuilder();
                for(int x=0;x<n.length;x++) { 
                    b.append(l[c[x]-'0'].charAt(n[x]));
                }
                result.add(b.toString());
                int p=0;
                boolean carry=false;
                n[0]+=1;
                while (n[p]==l[c[p]-'0'].length() && p < c.length) {
                    n[p]=0;
                    p+=1;
                    if (p==c.length) return result;
                    n[p]+=1;
                }
            }
        }
    }

Log in to reply
 

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