[LeetCode][Java] N-Queens II

题目:

Follow up for N-Queens problem.

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

题意:

接上题N-Queens problem,这里仅仅是要求返回全部的不同的解的数目。

算法分析:

思路与上题同样,代码比上题更加简洁了

AC代码:

<span style="font-size:12px;">public class Solution
{
    private int res=0;
    public int totalNQueens(int n)
    {
        int[] loc = new int[n];  //记录皇后处于哪一列,列数组
        dfs(loc,0,n);
        return res;
    }
    public void dfs(int[] loc, int cur, int n)
    {
        if(cur==n)
            res++;
        else
        {
            for(int i=0;i<n;i++)
            {
                loc[cur] = i;
                if(isValid(loc,cur))
                    dfs(loc,cur+1,n);  //再放皇后m+1, 假设皇后m+1放完并返回了
                                          //两种可能:  1:冲突,返回了  2.一直将所有的皇后所有放完并安全返回了
                                        //将皇后m回溯。探索新的可能或者安全的位置  --->
            }
        }
    }
    public boolean isValid(int[] loc, int cur)
    {
        for(int i=0;i<cur;i++)//仅仅须要保证与那些已经就位的皇后不冲突就可以
        {
            if(loc[i]==loc[cur]||Math.abs(loc[i]-loc[cur])==(cur-i)) //验证对角线,依据对角线性质,长 = 宽
                                                                     //那么我们不难写出 Math.abs(loc[i] - loc[cur]) == (cur - i)
                return false;
        }
        return true;
    }  

}</span>
时间: 2024-12-11 01:45:38

[LeetCode][Java] N-Queens II的相关文章

[LeetCode][Java] 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 ] ] 题意: 给定一个整数 n,生成一个正方形矩阵.矩阵中包含着从1到n2 这些元素,并且

[LeetCode][Java] 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 j

[Leetcode][JAVA] Word Ladder II

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 = "hit

[Leetcode][JAVA] 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"

[Leetcode][JAVA] Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",

[LeetCode][Java] Unique Paths II

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the m

[LeetCode][Java] Path Sum II

题目: 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] ] 题意: 给定一棵二叉树和一个和,找出从根节

[LeetCode][Java] Combination Sum II

题目: 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

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想法也是类似的,就是依照矩阵从外圈到内圈建立