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

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

N-Queens II

Follow up for N-Queens problem.

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

 

和上题几乎一样,还简单点。

1 class Solution { 2 private: 3   int res; 4 public: 5   int totalNQueens(int n) { 6     vector
state(n, -1); 7 res = 0; 8 helper(state, 0); 9 return res;10 }11 void helper(vector
&state, int row)12 {13 int n = state.size();14 if(row == n)15 {16 res++;17 return;18 }19 for(int col = 0; col < n; col++)20 if(isValid(state, row, col))21 {22 state[row] = col;23 helper(state, row+1);24 state[row] = -1;25 }26 }27 bool isValid(vector
&state, int row, int col)28 {29 for(int i = 0; i < row; i++)30 if(state[i] == col || abs(row - i) == abs(col - state[i]))31 return false;32 return true;33 }34 35 };

 

转载于:https://www.cnblogs.com/Sean-le/p/4784254.html

你可能感兴趣的文章
小米手机会不会更好
查看>>
atitit.Sealink2000国际海运信息管理系统
查看>>
android面试总结01 activity生命周期
查看>>
Java 实现策略(Strategy)模式
查看>>
Ubuntu离线安装Sogou拼音(附老版本安装&输入法自启动)
查看>>
springmvc结合base64存取图片到mysql
查看>>
深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow
查看>>
linux 抓包 tcpdump 简单应用
查看>>
mongodb官网文档阅读笔记:与写性能相关的几个因素
查看>>
PHP处理时间格式
查看>>
BestCoder Round #11 (Div. 2)
查看>>
JAVA入门[20]-Spring Data JPA简单示例
查看>>
Python: The _imagingft C module is not installed错误的解决
查看>>
HTTP请求报文和HTTP响应报文
查看>>
第3课 - 初识程序的灵魂
查看>>
WordPress插件扫描工具plecost
查看>>
【PDF】Java操作PDF之iText超入门
查看>>
PHP:第五章——字符串过滤函数
查看>>
Spring中ApplicationContextAware的用法
查看>>
flask的session解读及flask_login登录过程研究
查看>>