博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode – Refresh – N-Queens II
阅读量:7051 次
发布时间:2019-06-28

本文共 863 字,大约阅读时间需要 2 分钟。

You can use N-Queens I 's method counting for the result. But it wont pass the leetcode test.

Use bit operation will be much faster:

1 class Solution { 2 private: 3     int count, rec; 4 public: 5     void getQueen(int row, int ld, int rd) { 6         if (row == rec) { 7             count++; 8             return; 9         } else {10             int nonOccupied = rec & ~(row | rd | ld), occupy;11             while (nonOccupied != 0) {12                 occupy = nonOccupied & (~nonOccupied + 1);13                 nonOccupied -= occupy;14                 getQueen(row + occupy, (ld + occupy) << 1, (rd + occupy) >> 1);15             }16         }17     }18     int totalNQueens(int n) {19         count = 0;20         rec = (1 << n) - 1;21         getQueen(0, 0, 0);22         return count;23     }24 };

 

转载于:https://www.cnblogs.com/shuashuashua/p/4355163.html

你可能感兴趣的文章
Windows Server 2012 GUI与Core的切换
查看>>
CAS配置(二)
查看>>
Windows系统下JSP程序开发环境配置
查看>>
Spring Cloud的前世今生
查看>>
Android Studio 教程(干货)
查看>>
hadoop datanode 不能连接 namenode
查看>>
VyOS--企业软路由推荐
查看>>
node.js 在window下安装
查看>>
CentOS 7打开文件中文乱码
查看>>
Winform动态创建控件对DPI的处理
查看>>
new关键字与malloc的区别
查看>>
《Just For Fun》阅读摘抄
查看>>
hibernate4.3.5.Final入门1
查看>>
python 发送邮件模块
查看>>
unqlite安装/使用/测试
查看>>
SQLite 查询或更新上一条插入的数据
查看>>
Ansible 之 roles使用
查看>>
我的友情链接
查看>>
OpenCV+Dlib进行实时脸部检测
查看>>
【Android】简单的日志工具
查看>>