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竟然也能得到正确答案!我已惊呆!

  1. class Solution {
  2. public:
  3. vector<vector<string>> solveNQueens(int n) {
  4. vector<int> pos(n,-);
  5. helper(res,,pos,n);
  6. return res;
  7. }
  8. void helper(vector<vector<string>>& res,int index,vector<int>& pos,int len){
  9. if(index==len){
  10. vector<string> temp(len,string(len,'.'));
  11. for(int i=;i<len;i++){
  12. temp[i][pos[i]]='Q';
  13. }
  14. res.push_back(temp);
  15. }
  16. for(int col=;col<len;col++){
  17. if(isCheck(pos,index,col)){
  18. pos[index]=col;
  19. helper(res,index+,pos,len);
  20. pos[index]=-;
  21. }
  22. }
  23.  
  24. }
  25. bool isCheck(const vector<int>& pos,int row,int col){
  26. for(int i=;i<row;i++)
  27. if(col==pos[i]||abs(pos[i]-col)==abs(i-row))
  28. return false;
  29. return true;
  30. }
  31. private:
  32.  
  33. vector<vector<string>> res;
  34. };

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. 高效Web开发的10个jQuery代码片段

    原文转载:http://www.codeceo.com/article/10-jquery-snippets-web-dev.html

  2. CSS的clip-path 一

    首先介绍一下,我觉得前端开发都是很具有分享精神的,很多人都写出了很多优秀的总结经验供新手们参考,本人只是个搬运工,将别人优秀的文章进行了总结,本文主要转载自  大漠  的文章  http://www. ...

  3. Python—变量

    1.在Python中,变量名类似__xxx__的,也就是以双下划线开头,并且以双下划线结尾的,是特殊变量,特殊变量是可以直接访问的,不是private变量 2.访问限制: class内部属性可以被外部 ...

  4. Struts——(四)异常处理机制

    在通常的情况下,我们得到异常以后,需要将页面导航到一个错误提示的页面,提示错误信息.利用Stuts我们可以采用两种方式处理异常: 1.编程式异常处理 即我们在Action中调用业务逻辑层对象的方法时, ...

  5. JQuery Easy Ui DataGrid

    Extend from $.fn.panel.defaults. Override defaults with $.fn.datagrid.defaults. The datagrid display ...

  6. 创建并追加img元素(jquery!)

    有几种方法 但都需要你指定一个节点 根据这个节点进行添加 如现有一节点Id为pr:一,向该节点内部后方添加:1 $("#pr").append("<img src= ...

  7. EXISTS语句

    通常在我写EXISTS语句时,我会写成IF EXISTS(SELECT TOP(1) 1 FROM XXX),也没细细考究过为什么要这么写,只是隐约认为这样写没有啥问题,那今天就深究下吧! 首先准备测 ...

  8. 控制反转IOC与依赖注入DI

    理解 IOC  http://www.cnblogs.com/zhangchenliang/archive/2013/01/08/2850970.html IOC 相关实例      的http:// ...

  9. SVN服务器搭建之提交日志模版构建

    SVN服务器搭建之提交日志模版构建 日志提交有两种 一种是自己客户端设置提交日志模版,这个只适用于自己,没办法强制性运用到项目中,只能依照每个人的自觉性来处理. 第二种方法是SVN服务器设置提交日志模 ...

  10. WebService未能加载文件或程序集“ESRI.ArcGIS.XXX”或它的某一个依赖项

    开发环境:Windows7旗舰版64bit.VisualStudio2008 With SP1.ArcEngine10.0.NetFrameWork4.0.IIS7和C#开发语言. 编写ArcEngi ...