The output is different from my laptop


  • 0
    J

    The code is quite easy to understand.
    Output in my laptop: [a, b, c]
    But here: ["", "a","b","c"]

    Don't understand what's wrong.
    Code is:

     public List<String> letterCombinations(String digits) {
            String[] keys = {" ", "", "abc", "def", "ghi", "jkl",
                "mno","pqrs","tuv","wxyz"};
            int l = 0;
            List<StringBuffer> cdd = new ArrayList<StringBuffer>();
            List<StringBuffer> tmp = new ArrayList<StringBuffer>();
            cdd.add(new StringBuffer());
            
            while(l<digits.length()){
                String key = keys[digits.charAt(l)-'0'];
                String[] tokens = key.split("");
                for(String s: tokens){
                    for (StringBuffer sb: cdd){
                        StringBuffer ss = new StringBuffer(sb);
                        ss.append(s);
                        tmp.add(ss);
                    }
                }
                cdd.clear();
                cdd.addAll(tmp);
                tmp.clear();
                l++;
            }
            
            List<String> res = new ArrayList<String>();
            for(StringBuffer sb: cdd){
                res.add(sb.toString());
            }
            return res;
        }

Log in to reply
 

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