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 ...
随机推荐
- 高效Web开发的10个jQuery代码片段
原文转载:http://www.codeceo.com/article/10-jquery-snippets-web-dev.html
- CSS的clip-path 一
首先介绍一下,我觉得前端开发都是很具有分享精神的,很多人都写出了很多优秀的总结经验供新手们参考,本人只是个搬运工,将别人优秀的文章进行了总结,本文主要转载自 大漠 的文章 http://www. ...
- Python—变量
1.在Python中,变量名类似__xxx__的,也就是以双下划线开头,并且以双下划线结尾的,是特殊变量,特殊变量是可以直接访问的,不是private变量 2.访问限制: class内部属性可以被外部 ...
- Struts——(四)异常处理机制
在通常的情况下,我们得到异常以后,需要将页面导航到一个错误提示的页面,提示错误信息.利用Stuts我们可以采用两种方式处理异常: 1.编程式异常处理 即我们在Action中调用业务逻辑层对象的方法时, ...
- JQuery Easy Ui DataGrid
Extend from $.fn.panel.defaults. Override defaults with $.fn.datagrid.defaults. The datagrid display ...
- 创建并追加img元素(jquery!)
有几种方法 但都需要你指定一个节点 根据这个节点进行添加 如现有一节点Id为pr:一,向该节点内部后方添加:1 $("#pr").append("<img src= ...
- EXISTS语句
通常在我写EXISTS语句时,我会写成IF EXISTS(SELECT TOP(1) 1 FROM XXX),也没细细考究过为什么要这么写,只是隐约认为这样写没有啥问题,那今天就深究下吧! 首先准备测 ...
- 控制反转IOC与依赖注入DI
理解 IOC http://www.cnblogs.com/zhangchenliang/archive/2013/01/08/2850970.html IOC 相关实例 的http:// ...
- SVN服务器搭建之提交日志模版构建
SVN服务器搭建之提交日志模版构建 日志提交有两种 一种是自己客户端设置提交日志模版,这个只适用于自己,没办法强制性运用到项目中,只能依照每个人的自觉性来处理. 第二种方法是SVN服务器设置提交日志模 ...
- WebService未能加载文件或程序集“ESRI.ArcGIS.XXX”或它的某一个依赖项
开发环境:Windows7旗舰版64bit.VisualStudio2008 With SP1.ArcEngine10.0.NetFrameWork4.0.IIS7和C#开发语言. 编写ArcEngi ...