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. a标签中href的触发

    采用.trigger('click')没有效果,是用的$('xx')[0].click()来触发的.不知道为什么trigger不行,望指导.

  2. X-Frame-Options 响应头

    [X-Frame-Options 响应头] 参考:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/X-Frame-Options

  3. requireJS的引用

    main.js: require.config({ paths: { jquery: 'jquery-1.7.2', biz: 'biz', }}); require(['jquery', 'biz' ...

  4. String类的常用判断方法使用练习

    选取了一些常用的判断方法进行了使用练习,后续跟新其他方法 package StringDemo; // String类的判断方法解析 // 1:boolean equals(); // 判断字符串是否 ...

  5. 关于AFNetworking菊花转圈圈的问题

    刚开始接触AFN的时候,发现菊花老是不转,百度到大神的博客上,说AFN里面的API的网络加载菊花默认是关闭的(才发现默认情况下AFN的转圈圈的菊花是关闭的)需要手动打开才能够显示转圈圈,打开的方法是: ...

  6. VScode常用几个前端插件live HTML previewer和debugger for chrome的配置

    之前一直都是用sublime Text和chrome配合来写前端的页面,自从知道了有liveReload这个神奇的插件之后感觉爽翻了啊.好吧跑远了........ 话说最近微软搞了个VScode,听说 ...

  7. 'autocomplete="off"'在Chrome 中不起作用

    大家都知道autocomplete属性是表单字段中的HTML5新属性,该属性有两种状态值,分别为"on" 和 "off",该属性可省略:省略属性值后默认值为&q ...

  8. c语言冒泡排序

    在C语言中,常用的排序算法有:冒泡排序.快速排序.插入排序.选择排序.希尔排序.堆排序以及归并排序等等. 冒泡排序基本概念:  依次比较相邻的两个数,将小数放在前面,大数放在后面. #include ...

  9. 使用kindeditor文本编辑器

    aspx中代码: <%@ Page Language="C#" ValidateRequest="false" AutoEventWireup=" ...

  10. TCP/IP详解系列 --- 概念总结02

    TCP复位报文段(RST)的用途: 1.当客户端程序访问不存在的端口时,目标主机将给它发送一个复位报文段:收到复位报文段的一端应该关闭连接或者重新连接,而不能回应这个复位报文段. 2.当客户端程序向服 ...