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 ...
随机推荐
- c++ 中string用c输入输出
对于string自带的函数 c_str()返回的const char*类型,对于scanf函数不能使用,可以通过如下方法使用 string s; scanf("%s",&* ...
- npm 换源
npm config set registry https://registry.npm.taobao.org
- selenium—八种定位方法
find_element_by_id() find_element_by_name() find_element_by_class_name() find_element_by_tag_name() ...
- 【POJ2699】The Maximum Number of Strong Kings(二分,最大流)
题意: 有n个队伍,两两都有比赛 知道最后每支队伍获胜的场数 求最多有多少队伍,他们战胜了所有获胜场数比自己多的队伍,这些队伍被称为SK N<=50 思路:把每个队伍和它们两两之间的比赛都当做点 ...
- strcpy(),strcat()的用法
strcpy(): 定义一个字符串char a[20],和一个字符串c[]="i am a teacher!"; 把c复制到a中就可以这样用:strcpy(a,c); 这个函数包含 ...
- C语言解析Ini格式文件
引用别人的博文: http://www.open-open.com/lib/view/open1402278076447.html 可以解析 INI 格式的字符串.解析文件.保存到文件. 下面是头文件 ...
- 转载:shell脚本之sed使用----替换、变量、转义字符
sed替换的基本语法为:----s后面跟的是分隔符,原字符串可使用.*这种正则表达式进行整行替换 代码如下: sed 's/原字符串/替换字符串/' 单引号里面,s表示替换,三根斜线中间是替换的样式, ...
- let it be
回家路上听到电台里主持人在介绍这首歌,听得我两眼模糊,真的太应最近的心情了. let it be.
- Android开发学习---如何写数据到外部存储设备(sd卡),Environment.getExternalStorageDirectory,怎么获取sd卡的大小?
本文主要介绍如何写数据到sd卡,这里主要到的技术是Environment中的方法. 1. 2.实现代码: /datasave/src/com/amos/datasave/savePasswordSer ...
- clob型不能用 distinct,以及转换clob类型方法
举例clob型不能用 distinct public List<WorkingPaper> findAssignedWorkPapers(String projectId, String ...