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 ...
随机推荐
- 用node搭建静态文件服务器
占个坑,写个node静态文件服务器
- 安装Linux系统Fedora 23
搭建了一个Linux(Fedora 23)环境,用于学习.实验. [TOC] 1 安装Fedora 以前一直只会光盘安装,刻录了好多个版本的distros,用完即弃在一旁.很浪费. 此次学会了在Lin ...
- miniui中常用的状态显示方式
1.查询sys_code表得到对应的状态 考生状态:<input class="mini-combobox" style="" textField=&qu ...
- C# 获取excel架构并的导入sqlserver的方法
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Codeforces Round #384 (Div. 2) //复习状压... 罚时爆炸 BOOM _DONE
不想欠题了..... 多打打CF才知道自己智商不足啊... A. Vladik and flights 给你一个01串 相同之间随便飞 没有费用 不同的飞需要费用为 abs i-j 真是题意杀啊, ...
- silverlight控件阴影效果示例
<ScrollViewer MaxHeight="400" VerticalScrollBarVisibility="Auto" HorizontalSc ...
- 总结--解决 mysql 中文乱码
首先分析一下导致mysql 中文乱码的原因: 1.建表时使用了latin 编码 2.连接数据库的编码没有指定 3.写入时就已经乱码(这种情况需要自己检查源数据了) 解决方法总结: 1.创建库时指定编码 ...
- L440 无线网卡:由于该设备有问题,Windows 已将其停止(代码 43)
最近重装了系统,本来用的好好的,结果重启之后突然无线网卡不能用了,设备管理器老是黄色叹号!无线网卡设备状态:由于该设备有问题,Windows 已将其停止. (代码 43). 无线网卡型号:2 ...
- (转)SQL Server 性能调优(cpu)
摘自:http://www.cnblogs.com/Amaranthus/archive/2012/03/07/2383551.html 研究cpu压力工具 perfom SQL跟踪 性能视图 cpu ...
- Python 基本语法 学习之路(三)
定义变量 在Python中,定义一个变量是很简单的.而且,在Python中,定义是不需要用分号结尾的.例如: a = 10 b = 3 print(a*b) 判断语句 Pyhon的if判断语句是由if ...