[leedcode 52] N-Queens II

Follow up for N-Queens problem.

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

public class Solution {
    //本题类似于N-Queens,这个更简单一些,只需要求出解法的个数即可,因此没有了print步骤
    //注意:isvalid函数的参数,以及函数中i的范围
    //      A[]的意义,可以代表棋盘的行
    int res;
    int A[];
    public int totalNQueens(int n) {
        A=new int[n];
        nqueens(0,n);
        return res;

    }
    public void nqueens(int cur,int n){
        if(cur==n){
            res++;
        }else{
            for(int i=0;i<n;i++){
                A[cur]=i;
                if(isValid(cur)){
                   nqueens(cur+1,n);
                }
            }
        }
    }
    public boolean isValid(int cur){
        for(int i=0;i<cur;i++){
            if(A[i]==A[cur]||Math.abs(A[i]-A[cur])==cur-i)
                return false;
        }
        return true;
    }
}
时间: 2024-10-28 21:24:23

[leedcode 52] N-Queens II的相关文章

[leetcode] 52. N皇后 II

52. N皇后 II 跟上个题一模一样,现在只需输出个数即可 class Solution { public int totalNQueens(int n) { boolean[] row = new boolean[n]; boolean[] h = new boolean[2 * n]; boolean[] r = new boolean[2 * n]; List<List<String>> ans = new ArrayList<>(); dfs(n, row,

lintcode 中等题:N Queens II N皇后问题 II

题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递归的时候才能保存结果,参考程序 java程序: class Solution { /** * Calculate the total number of distinct N-Queen solutions. * @param n: The number of queens. * @return:

[LeetCode 51&amp;52] N-Queens I &amp; II (N皇后问题)

题目链接:n-queens import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * 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 dist

[leedcode 210] Course Schedule II

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a l

[leedcode 140] Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

[leedcode 213] House Robber II

Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place arearranged in a circle. T

[leedcode 137] Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. public class Solution { public int singleNumber(int[] nums) { //int的32个bit逐个处理,每一个bit进行相加,然后mod3,即可判断多余的一位是1或0.此思想同样适用于均出现三次,一个出现两次场景 int res=0; for(i

[leedcode 45] Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

[leedcode 59] Spiral Matrix II

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 ] ] public class Solution { int res[][]; int val=1;