Skip to content

Commit 4c166e0

Browse files
committed
Added NQueens Leetcode Solution
1 parent 40c0e9c commit 4c166e0

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

LeetCode/NQueens.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public List<List<String>> solveNQueens(int n) {
2121

2222
public void findNQueens(List<List<String>> l, int n, int k, int a[])
2323
{
24+
// if k == n then it means we have successfully placed all
25+
// the N Queens so we add it to the resultant list
2426
if(k == n)
2527
{
2628
List<String> board = new ArrayList<String>();
@@ -44,6 +46,8 @@ public void findNQueens(List<List<String>> l, int n, int k, int a[])
4446
l.add(board);
4547
}
4648

49+
// The for loop traverses the entire matrix placing each
50+
// queen at a position and checking if it is valid
4751
for(int i = k; i < n; i++)
4852
{
4953
for(int j = 0; j < n; j++)
@@ -60,6 +64,8 @@ public void findNQueens(List<List<String>> l, int n, int k, int a[])
6064
}
6165
}
6266

67+
//This function checks whether placing Queen at a particular
68+
// position affects previously placed Queens
6369
public boolean isValid(int i, int j, int a[])
6470
{
6571
for(int k = 0; k < i; k++)

0 commit comments

Comments
 (0)