287. Find the Duplicate Number hard
287. Find the Duplicate Number hard
http://www.cnblogs.com/grandyang/p/4843654.html
51. N-Queens
http://blog.csdn.net/linhuanmars/article/details/20667175 http://www.cnblogs.com/grandyang/p/4377782.html
思路就是利用一个pos[row]=col来记录 行号:row,Queen在第col列。
再用一个index来记录当前递归到行号:index。
另外检查时候,只需要传入pos数组跟当前的位置 row,col。并且只需要从第0行倒第row-1行即可。
其中,判断当前行是不需要的,判断当前列跟斜线即可。斜线上直接利用斜率即可:(y2-y1)/(x2-x1)=正负1即可!
不过helper函数中第一个if语句没有 return竟然也能得到正确答案!我已惊呆!
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<int> pos(n,-);
helper(res,,pos,n);
return res;
}
void helper(vector<vector<string>>& res,int index,vector<int>& pos,int len){
if(index==len){
vector<string> temp(len,string(len,'.'));
for(int i=;i<len;i++){
temp[i][pos[i]]='Q';
}
res.push_back(temp);
}
for(int col=;col<len;col++){
if(isCheck(pos,index,col)){
pos[index]=col;
helper(res,index+,pos,len);
pos[index]=-;
}
}
}
bool isCheck(const vector<int>& pos,int row,int col){
for(int i=;i<row;i++)
if(col==pos[i]||abs(pos[i]-col)==abs(i-row))
return false;
return true;
}
private:
vector<vector<string>> res;
};
287. Find the Duplicate Number hard的更多相关文章
- leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array
后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...
- LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- [LeetCode] 287. Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- [LeetCode] 287. Find the Duplicate Number 解题思路
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode 287. Find the Duplicate Number (找到重复的数字)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 287. Find the Duplicate Number 找出数组中的重复数字
[抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- 【LeetCode】287. Find the Duplicate Number
Difficulty:medium More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...
- [LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)
传送门 Description Given an array nums containing n + 1 integers where each integer is between 1 and n ...
随机推荐
- cf730e
一道数学题,这篇博客很好:http://blog.csdn.net/morejarphone/article/details/52926627(这样应该不算转载吧) 总结:做这类题目显然应该直接根据相 ...
- 在powerdesigner中,一个table,怎么在diagram中创建多个symbol
两种方式 第一:可以创建多个diagram,直接把表拖到diagram中就可以 第二:复制->粘贴快捷方式,或者Ctrl+C先复制,再Ctrl+K粘贴到Diagram中 说明: ctrl+V 是 ...
- IT学习网站集结
IT的学习网站: 慕课网 http://www.imooc.com 51CTO http://www.51cto.com/ CSDN http://www.csdn.net/ 极客 ...
- 阿里云 ECS 逻辑卷挂载数据盘
查看磁盘信息: [root@ixx~]# fdisk -l Disk /dev/xvda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track ...
- openvpn安装
1,wget http://swupdate.openvpn.org/as/openvpn-as-2.0.10-CentOS7.x86_64.rpm 2,passwd openvpn
- jquery取框架当前的url
前提,当前frame无名或动态生成的: var url = eval($("#ifr1").attr("name")+".location.href; ...
- myeclipse部署web项目到server : deploy location 为 INVALID,并且不能更改
首先查看项目路径下有没有 .mymetadata文件,没有就从其它地方挪一个,但是文件中的id必须是唯一的. 如果还不行就去下面这位大哥写的博客看下,进行下一步就可以解决了 生如夏花
- 03 Apache Solr: 安装和运行
前面介绍了Solr在项目中的使用和构建高度可用.高度可扩展的Solr服务器的一些想法.但是光说不练假把式,现在开始,把Solr运行起来继续深入了解吧! 安装 安装JAVA Apache So ...
- MAC 安装 Protobuf
1.确认MAC装有g++.make.vim工具 2.安装make工具使用 brew install make 3.安装protobuf brew install protobuf 4.安装 ...
- Java(异常处理)动手动脑
1>请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. import javax.swing.*; class AboutEx ...