`The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.`

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where `'Q'` and `'.'` both indicate a queen and an empty space respectively.

For example,
There exist two distinct solutions to the 4-queens puzzle:

```[
[".Q..",  // Solution 1
"...Q",
"Q...",
"..Q."],

["..Q.",  // Solution 2
"Q...",
"...Q",
".Q.."]
]```

``` 1 public class Solution {
2     public ArrayList<String[]> solveNQueens(int n) {
3         ArrayList<String[]> res = new ArrayList<String[]>();
4         helper(n, 0, new int[n], res);
5         return res;
6     }
7
8     public void helper(int n, int currentrow, int[] colforrow, ArrayList<String[]> res) {
9         if (currentrow == n) { //find a suitable solution, add to result list
10             String[] array = new String[n];
11             for (int k = 0; k < n; k++) {
12                 StringBuffer buff = new StringBuffer();
13                 for (int m = 0; m < n; m++) {
14                     if (colforrow[k] == m) buff.append('Q');
15                     else buff.append('.');
16                 }
17                 String st = buff.toString();
18                 array[k] = st;
19             }
21             return;
22         }
23
24         for (int i = 0; i < n; i++) {
25             colforrow[currentrow] = i;
26             if (check(currentrow, colforrow)) {
27                 helper(n, currentrow+1, colforrow, res);
28             }
29         }
30     }
31
32     public boolean check(int currentrow, int[] colforrow) {
33         for (int temp = 0; temp < currentrow; temp++) {
34             if (colforrow[temp] == colforrow[currentrow] || Math.abs(colforrow[temp] - colforrow[currentrow]) == Math.abs(temp - currentrow))
35                 return false;
36         }
37         return true;
38     }
39 }```