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的更多相关文章

  1. 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 ...

  2. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  3. [LeetCode] 287. Find the Duplicate Number 寻找重复数

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  4. 287. Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  5. [LeetCode] 287. Find the Duplicate Number 解题思路

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  6. LeetCode 287. Find the Duplicate Number (找到重复的数字)

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  7. 287. Find the Duplicate Number 找出数组中的重复数字

    [抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  8. 【LeetCode】287. Find the Duplicate Number

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...

  9. [LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)

    传送门 Description Given an array nums containing n + 1 integers where each integer is between 1 and n  ...

随机推荐

  1. 在做基于LBS应用的一些随笔

    公司做了一个基于LBS的APP,在做服务端的时候出现了一些注意事项,还是记录下把. 首先是关于坐标: 弧长公式:L=nπr/180°或l=|α|r.地球半径大致是6400千米.以纬度0.000001为 ...

  2. iOS中UITableView使用总结

    链接:http://www.open-open.com/lib/view/open1430008922468.html

  3. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->使用Spring Framework开发自己的应用程序

    1.直接基于spring framework开发自己的应用程序: 1.1参考资料: Spring官网spring-framework.4.3.5.RELAESE的Reference Documenta ...

  4. VueJS取得URL参数

    vuejs取得URL中参数的值 地址:http://localhost:3333/#/index?id=001 结果:001 console.log(this.$route.query.id)

  5. 【总结】清除webbrowser cookie/session的6种方法

    下面是我测试下来的6种清除webbrowser中cookie的6种方法: //方法一:调用 wininet.dll清除cookie (推荐) SuppressWininetBehavior(); // ...

  6. SUSE zypper failed to work

    记录解决的一个问题. 在SUSE 中zypper 不能使用,错误如下: hostname~ # zypper install make Refreshing service 'packman'.Une ...

  7. android中回调函数机制完全解析

    1.在要调用的业务操作中,创建一个接口,在接口中创建方法,这个方法表示的是我们原先要在业务类中执行的操作 public interface BackUpSmsListener { /** * 设置总进 ...

  8. xml的解析与创建——bing到youdao导入文件的转换

    首先是为了解决一个问题:如何将必应单词本中记录的单词转入到有道词典中去.实际上,必应词典可以导出xml文件,但是该文件有道词典无法解析.这里涉及到xml的解析和创建了. 代码如下: import ja ...

  9. ext.ajax.request请求时带有遮罩效果

    ajax请求时有时需要操作大量的数据,反应有时会很慢,这时我们想要来一个遮罩效果,具体步骤如下 1.定义一个遮罩 var myMask = new Ext.LoadMask(Ext.getBody() ...

  10. vi

    e! 放弃所有修改,从上次保存文件开始再编辑 shift+g 最后一行 gg 第一行 u 恢复上一次操作 如果查找下一个,按"n"即可. set nu 显示行号 编辑模式下111g ...