Share my 16-line java code


  • 1
    M
    public class Solution {
        public List<String> findStrobogrammatic(int n) {
            return generate(n, 0);
        }
        
        private List<String> generate(int remain, int level) {
            if (remain == 0) { return new ArrayList<String>(Arrays.asList("")); }
            if (remain == 1) { return new ArrayList<String>(Arrays.asList("0", "1", "8")); }
            List<String> curResults = new ArrayList<>();
            List<String> prevResults = generate(remain-2, level+1);
            for (String s: prevResults) {
                if (level > 0) { curResults.add("0" + s + "0"); }
                curResults.add("1" + s + "1");
                curResults.add("6" + s + "9");
                curResults.add("8" + s + "8");
                curResults.add("9" + s + "6");
            }
            return curResults;
        }
    }

Log in to reply
 

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