leetcode 52 N皇后问题 II

51的简化版,省去根据排列话棋盘的工作,直接计数,代码:

class Solution {
public:
    int totalNQueens(int n) {
        int res=0;
        vector<int> pos(n,-1);
        dfs(n,0,pos,res);
        return res;
    }
    void dfs(int n,int row,vector<int>& pos,int &res){
        if(row==n){
            res++;return;
        }
        for(int col=0;col<n;col++){
            if(isValid(row,col,pos)){
                pos[row]=col;
                dfs(n,row+1,pos,res);
                pos[row]=-1;
            }
        }
    }
    bool isValid(int row,int col,vector<int>&pos){
        for(int i=0;i<row;i++){
            if(col==pos[i] || abs(col-pos[i])==abs(row-i))
                return false;
        }
        return true;
    }
};

原文地址:https://www.cnblogs.com/joelwang/p/10699042.html

时间: 2024-11-06 13:47:22

leetcode 52 N皇后问题 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,

8.18 [LeetCode 52] N-Queens II

[LeetCode 52] N-Queens II | COMMENTS Question link Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. Stats Frequency 3 Difficulty 4 Adjusted Difficulty 2 Time to use ——– Ratin

leetcode之n皇后问题

leetcode上有两个关于n皇后的问题,两个题目基本是一样的,只是第二个是把所有的排法求出来.n皇后最简单的就是用递归,每次判断一行的一个位置,如果合法,就判断下一行,不合法再判断下一个位置 N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. class Solution {

Java [Leetcode 119]Pascal&#39;s Triangle II

题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

leetcode 【 Pascal&#39;s Triangle II 】python 实现

题目: 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? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege

[LeetCode] Unique Binary Search Trees II (难以忍受的递归)

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 class Solution { private

leetcode 119 Pascal&#39;s Triangle II ----- 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? 上一道题的延伸版,就是直接求出第k行的数,要求用o(k)的空间复杂度. 也是直接相加就可以了. public class Solution { pub

leetCode 119. Pascal&#39;s Triangle II 数组

119. Pascal's Triangle II 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? 代码如下:(使用双数组处理,未优化版) class Solution { public:     

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: