Thoughts on this Java Solution (not the prettiest)


  • 0
    S

    I was having trouble coming up with a solution so I solved it like this. I looked around and didn't really see another answer like mine.

    I feel like my code could be cleaned up but it's pretty straight forward.

    public class Solution {
        public List<String> findStrobogrammatic(int n) {
            
            List<String> output = new ArrayList<>(); 
            
            return helper(n, output, "");
            
        }
        
        public List<String> helper(int n, List<String> output, String s) {
            
            if (n == 0) {
                output.add(s); 
                return output;
            }
            
            if (n % 2 == 1) {
                String temp = "1" + s;
                helper(n - 1, output, temp);
                
                temp = "0" + s;
                helper(n - 1, output, temp);
                
                temp = "8" + s;
                return helper(n - 1, output, temp);
            }
            
            if (n > 2) {
                String temp = "0" + s + "0";
                helper(n-2, output, temp);
            }
            
            String temp = "1" + s + "1";
            helper(n - 2, output, temp);
            
            temp = "6" + s + "9";
             helper(n - 2, output, temp);
            
            temp = "9" + s + "6";
            helper(n - 2, output, temp);
            
            temp = "8" + s + "8";
            return helper(n - 2, output, temp);
        }
    }

Log in to reply
 

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