N-Queens II leetcode java

题目:

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

题解:

这道题跟NQueens的解法完全一样(具体解法参照N QueensN Queens leetcode java),只不过要求的返回值不同了。。所以要记录的result稍微改一下就好了。。。

因为涉及到递归,result传进去引用类型(List,数组之类的)才能在层层递归中得以保存,所以这里使用一个长度为1的数组帮助计数。

当然,也可以使用一个全局变量来帮助计数。

代码如下:

1     public int totalNQueens(int n) {  
 2         int[] res = {0};
 3         if(n<=0)
 4             return res[0];
 5             
 6         int [] columnVal = new int[n];
 7         
 8         DFS_helper(n,res,0,columnVal);
 9         return res[0];
10     }
11     
12     public void DFS_helper(int nQueens, int[] res, int row, int[] columnVal){
13         if(row == nQueens){
14             res[0] += 1;
15         }else{
16             for(int i = 0; i < nQueens; i++){
17                 columnVal[row] = i;//(row,columnVal[row)==>(row,i)
18                 
19                 if(isValid(row,columnVal))
20                     DFS_helper(nQueens, res, row+1, columnVal);
21             }
22         }
23     }
24     
25     public boolean isValid(int row, int [] columnVal){
26         for(int i = 0; i < row; i++){
27             if(columnVal[row] == columnVal[i]
28                ||Math.abs(columnVal[row]-columnVal[i]) == row-i)
29                return false;
30         }
31         return true;

使用全局变量来记录结果的代码是:

1     int res;
 2     public int totalNQueens(int n) { 
 3         res = 0;
 4         if(n<=0)
 5             return res;
 6             
 7         int [] columnVal = new int[n];
 8         
 9         DFS_helper(n,0,columnVal);
10         return res;
11     }
12     
13     public void DFS_helper(int nQueens, int row, int[] columnVal){
14         if(row == nQueens){
15             res += 1;
16         }else{
17             for(int i = 0; i < nQueens; i++){
18                 columnVal[row] = i;//(row,columnVal[row)==>(row,i)
19                 
20                 if(isValid(row,columnVal))
21                     DFS_helper(nQueens, row+1, columnVal);
22             }
23         }
24     }
25     
26     public boolean isValid(int row, int [] columnVal){
27         for(int i = 0; i < row; i++){
28             if(columnVal[row] == columnVal[i]
29                ||Math.abs(columnVal[row]-columnVal[i]) == row-i)
30                return false;
31         }
32         return true;
33     }

时间: 2024-11-06 01:00:39

N-Queens II leetcode java的相关文章

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

Combination Sum II leetcode java

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

Reverse Linked List II leetcode java

题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n

Word Ladder II leetcode java

题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start =

Populating Next Right Pointers in Each Node II leetcode java

题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following bina

Unique Binary Search Trees II leetcode java

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题解:这道题比1难的就是不是返回个数,而

Path Sum II leetcode java

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 题解: 这道题除了要判断是否有这样的一个p

Subset II leetcode java

题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solution