ITPub博客

首页 > > > [LeetCode] N-Queens II

[LeetCode] N-Queens II

作者:Ruiyuan Li 时间:2015-08-22 00:00:00 0 删除 编辑

N-Queens II

Follow up for N-Queens problem.

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

解题思路:

这道题与http://blog.csdn.net/kangrydotnet/article/details/47857469类似,用它的第3个方法计算非常快。

class Solution {
public:
    int totalNQueens(int n) {
        if(n<=0){
            return 0;
        }
        int result = 0;
        
        helper(result, n, 0, 0, 0, 0);
        
        return result;
    }
    
    void helper(int& result, int n, int row, int d, int l, int r){
        if(row == n){
            result++;
            return;
        }
        int p = ((1 << n) - 1) & ~(d | l | r);
        int i = 0;
        while(p){
            if(p&1){
                int m = 1<> 1);
            }
            i++;
            p = p>>1;
        }
    }
};


转载请注明: » [LeetCode] N-Queens II
<!-- -->

原文作者:;如有侵权,请联系管理员删除。

上一篇: 关于部落
请登录后发表评论 登录
全部评论

注册时间:2020-05-21

  • 博文量
    495
  • 访问量
    4825