Java Concise 4ms 99% Sol.


  • 0
    O
    public class Solution {
        public List<String> findStrobogrammatic(int n) {
            char[] c = new char[n];
            int l = (n - 1) / 2, r = l + (n - 1) % 2;
            List<String> ans = new LinkedList();
            findStrobo(c, l, r, ans);
            return ans;
        }
        
        private static char[][] pair = {{'0', '0'}, {'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
        
        public void findStrobo(char[] c, int l, int r, List<String> ans) {
            if (l < 0) {
                if (c[0] != '0' || c.length == 1)
                    ans.add(new String(c));
                return;
            }
            
            int m = (l == r) ? 3 : 5;
            for (int i = 0; i < m; i++) {
                c[l] = pair[i][0];
                c[r] = pair[i][1];
                findStrobo(c, l - 1, r + 1, ans);
            }
        }
    }
    

Log in to reply
 

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