Very Clean JAVA 16ms Solution, Easy Understand


  • 0
    Y
    public class Solution {
        public List<String> findStrobogrammatic(int n) {
            List<String> res = new ArrayList<String>();
    
            if(n % 2 == 0)
                getRes(n, "", res);
            else {
                getRes(n - 1, "1", res);
                getRes(n - 1, "0", res);
                getRes(n - 1, "8", res);
            }
            
            return res;
        }
        
        public void getRes(int n, String s, List<String> res) {
            if(n == 0) {
                res.add(s);
                return;
            }
            if(n != 2) {
                getRes(n - 2, "0" + s + "0", res);
            }
            
            getRes(n - 2, "6" + s + "9", res);
            getRes(n - 2, "9" + s + "6", res);
            getRes(n - 2, "1" + s + "1", res);
            getRes(n - 2, "8" + s + "8", res);
                
            return;
        }
    }

Log in to reply
 

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