Java Solution Beats 90%


  • 0
    L
    public List<String> findStrobogrammatic(int n) {
        List<String> res = new LinkedList<>();
        String[] pair = {"00", "11", "88", "69", "96"};
        char[] str = new char[n];
        helper(res, str, pair, 0, n, n);
        return res;
    }
    
    public void helper(List<String> res, char[] str, String[] pair, int idx, int length, int n) {
        if (length == 0) {
            res.add(String.valueOf(str));
            return;
        }
        if (length == 1) {
            for (int i = 0; i <= 2; i++) {
                str[idx] = pair[i].charAt(0);
                res.add(String.valueOf(str));
            }
            return;
        }
        int start = length == n ? 1 : 0;
        for (int i = start; i < pair.length; i++) {
            str[idx] = pair[i].charAt(0);
            str[idx + length - 1] = pair[i].charAt(1);
            helper(res, str, pair, idx + 1, length - 2, n);
        }
    }

Log in to reply
 

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