8 ms Easy-to-understand Java Solution


  • 0
    O
    public List<String> findStrobogrammatic(int n) {
            List<String> stroboNums = new ArrayList<>();
            stroboCombs(stroboNums, 0, n-1, new char[n]);
            return stroboNums;
    
        }
    
        private void stroboCombs(List<String> stroboNums, int l, int r, char[] strobo) {
            if (l > r) {
                stroboNums.add(new String(strobo));
            } else {
    
                for(char c='0'; c<='9'; c++) {
                    if (c == '0' && (l > 0 || (l == 0 && l == r))) {
                        strobo[l] = '0';
                        strobo[r] = '0';
                        stroboCombs(stroboNums, l+1, r-1, strobo);
                    }
                    if (c == '6' && l != r) {
                        strobo[l] = '6';
                        strobo[r] = '9';
                        stroboCombs(stroboNums, l+1, r-1, strobo);
                    } else if (c == '9' && l != r) {
                        strobo[l] = '9';
                        strobo[r] = '6';
                        stroboCombs(stroboNums, l+1, r-1, strobo);
                    } else if (c == '1' || c == '8'){
                        strobo[l] = c;
                        strobo[r] = c;
                        stroboCombs(stroboNums, l+1, r-1, strobo);
                    }
    
                }
            }
        }
    

Log in to reply
 

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