Strobogrammatic Number II

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Find all strobogrammatic numbers that are of length = n.

For example,
Given n = 2, return ["11","69","88","96"].

    public List<String> findStrobogrammatic(int n) {
        List<String> res = new ArrayList();
        if(n <= 0) return res;
        Helper(res, new StringBuilder(), n);
        return res;
    }

    private void Helper(List<String> res, StringBuilder comb, int n){
        int len = comb.length();
        if(len > 0 && len >= n / 2.0){
            for(int i = len + 1; i <= n ; i++)
                comb.append(getStroNum(comb.charAt(n - i)));
            res.add(comb.toString());
        }else {
            char []stroNum = {'0', '1', '6', '8', '9'};
            for(int i = 0 ; i < stroNum.length; i++){
                if(i == 0 && len == 0 && n > 1)
                    continue;
                if((i == 2 || i == 4) && ((n & 1) == 1 && len == (1 + n) / 2 - 1))
                    continue;
                StringBuilder sb = new StringBuilder(comb);
                sb.append(stroNum[i]);
                Helper(res, sb, n);
            }

        }
    }

    private char getStroNum(char n){
        switch (n){
            case '0': return '0';
            case '1': return '1';
            case '6': return '9';
            case '8': return '8';
            case '9': return '6';
        }
        return ' ';
    }

results matching ""

    No results matching ""