@@ -0,0 +1,1628 @@
+package leetcode;%0A%0A/**%0A * https://leetcode.com/problems/n-queens-ii/%0A */%0Apublic class Problem52 %7B%0A    public int totalNQueens(int n) %7B%0A        char%5B%5D%5B%5D board = new char%5Bn%5D%5Bn%5D;%0A        for (int i = 0; i %3C board.length; i++) %7B%0A            for (int j = 0; j %3C board%5Bi%5D.length; j++) %7B%0A                board%5Bi%5D%5Bj%5D = '.';%0A            %7D%0A        %7D%0A        return nQueens(board, 0);%0A    %7D%0A%0A    private int nQueens(char%5B%5D%5B%5D board, int row) %7B%0A        if (row == board.length) %7B%0A            return 1;%0A        %7D%0A        int result = 0;%0A        for (int col = 0; col %3C board%5Brow%5D.length; col++) %7B%0A            if (!isPossible(board, row, col)) %7B%0A                continue;%0A            %7D%0A            board%5Brow%5D%5Bcol%5D = 'Q';%0A            result += nQueens(board, row + 1);%0A            // reset everything%0A            board%5Brow%5D%5Bcol%5D = '.';%0A        %7D%0A        return result;%0A    %7D%0A%0A    private boolean isPossible(char%5B%5D%5B%5D board, int row, int col) %7B%0A        // up row%0A        for (int i = row; i %3E= 0; i--) %7B%0A            if (board%5Bi%5D%5Bcol%5D == 'Q') %7B%0A                return false;%0A            %7D%0A        %7D%0A        // left diagonal%0A        for (int i = row, j = col; i %3E= 0 && j %3E= 0; i--, j--) %7B%0A            if (board%5Bi%5D%5Bj%5D == 'Q') %7B%0A                return false;%0A            %7D%0A        %7D%0A        // right diagonal%0A        for (int i = row, j = col; i %3E= 0 && j %3C board.length; i--, j++) %7B%0A            if (board%5Bi%5D%5Bj%5D == 'Q') %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem52 prob = new Problem52();%0A        System.out.println(prob.totalNQueens(8));%0A    %7D%0A%7D%0A

Solve problem 52