Skip to content

Commit c738ca4

Browse files
Time: 4 ms (19.74%), Space: 40.5 MB (59.38%) - LeetHub
1 parent 8b2ceb9 commit c738ca4

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

0052-n-queens-ii/0052-n-queens-ii.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
class Solution {
2-
int count = 0;
3-
4-
public int totalNQueens(int n) {
5-
char[][] board=new char[n][n];
6-
2+
int count = 0;
3+
public int totalNQueens(int n) {
4+
char board[][] = new char[n][n];
5+
76
for(int i = 0; i < n; i++){
8-
9-
for(int j = 0; j < n; j++){
7+
for(int j = 0;j < n; j++){
108
board[i][j]='.';
119
}
1210
}
@@ -15,46 +13,43 @@ public int totalNQueens(int n) {
1513
return count;
1614
}
1715

18-
public boolean boardgenerater(char[][] board,int col, int n){
16+
private boolean boardgenerater(char board[][],int col,int n){
1917
if(col == n){
2018
count++;
2119
return false;
2220

2321
}
24-
22+
2523
for(int i = 0; i < n; i++){
2624

2725
if(issafe(board, i, col, n)){
2826
board[i][col] = 'Q';
2927

30-
if(boardgenerater(board, col + 1, n)){
28+
if( boardgenerater(board, col + 1, n)){
3129
return true;
3230
}else{
33-
board[i][col]='.';
31+
board[i][col] = '.';
3432
}
3533
}
3634
}
3735
return false;
3836
}
3937

40-
public boolean issafe(char[][] board, int row, int col, int n){
38+
private boolean issafe(char board[][], int row, int col, int n){
4139
int x = row;
4240
int y = col;
4341

4442
for(int i = 0; i < n; i++){
45-
46-
if(board[i][col] == 'Q'){
43+
if(board[i][col ]== 'Q'){
4744
return false;
4845
}
4946

5047
if(board[row][i] == 'Q'){
5148
return false;
5249
}
53-
5450
}
5551

5652
while(x < n && y >= 0){
57-
5853
if(board[x][y] == 'Q'){
5954
return false;
6055
}
@@ -63,8 +58,7 @@ public boolean issafe(char[][] board, int row, int col, int n){
6358
}
6459

6560
while(row >= 0 && col >= 0){
66-
67-
if(board[row][col] =='Q'){
61+
if(board[row][col] == 'Q'){
6862
return false;
6963
}
7064
row--;

0 commit comments

Comments
 (0)