LeetCode OJ--N-Queens II
https://oj.leetcode.com/problems/n-queens-ii/
N皇后问题,计算解的个数
class Solution {
public:
int totalNQueens(int n) {
if(n<=)
return ;
vector<int> places(n,-);
int ans = ;
placeQueen(, n, ans, places);
return ans;
}
// col and lines
bool valid(int i, int j, vector<int> &places)
{
for(int p = ; p<i; p++)
{
if(places[p] == j || abs(i - p) == abs(places[p] - j))
return false;
}
return true;
}
void placeQueen(int i, int n, int &ans, vector<int> &places)
{
// one solution
if(i == n)
{
ans++;
}
for(int col = ; col < n; col++)
{
if(valid(i,col,places))
{
places[i] = col;
placeQueen(i+,n,ans,places); // next queue, next line
}
}
}
};
LeetCode OJ--N-Queens II的更多相关文章
- LeetCode OJ Basic Calculator II
Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: strin ...
- LeetCode OJ 47. Permutations II
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...
- LeetCode OJ:Permutations II(排列II)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode OJ:Subsets II(子集II)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
随机推荐
- [译]The Python Tutorial#3. An Informal Introduction to Python
3. An Informal Introduction to Python 在以下示例中,输入和输出以提示符(>>>和...)的出现和消失来标注:如果想要重现示例,提示符出现时,必须 ...
- 学习Spring框架系列(一):通过Demo阐述IoC和DI的优势所在
Spring框架最核心东西便是大名鼎鼎的IoC容器,主要通过DI技术实现.下面我通过Demo的演变过程,对比学习耦合性代码,以及解耦和的过程,并深入理解面向接口编程的真正内涵. 这个例子包括如下几个类 ...
- Python之路-基础数据类型之字符串
字符串类型 字符串是不可变的数据类型 索引(下标) 我们在日常生活中会遇到很多类似的情况,例如吃饭排队叫号,在学校时会有学号,工作时会有工号,这些就是一种能保证唯一准确的手段,在计算机中也是一样,它就 ...
- C语言中可变参数的使用
在C语言程序编写中我们使用最多的函数一定包括printf以及很多类似的变形体.这个函数包含在C库函数中,定义为 int printf( const char* format, ...); 除了一个格式 ...
- loj2091 「ZJOI2016」小星星
ref 总的来说,就是 容斥转化为点对应到点集问题. 树形 dp 解决转化后的问题. #include <iostream> #include <cstring> #inclu ...
- laravel5.2总结--csrf保护
CSRF攻击: CSRF 顾名思义,是伪造请求,冒充用户在站内的正常操作.我们知道,绝大多数网站是通过 cookie 等方式辨识用户身份(包括使用服务器端 Session 的网站,因为 S ...
- leetcode 【 Triangle 】python 实现
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- IOS笔记050-事件处理
IOS事件处理 1.触摸事件 2.加速器事件:重力感应,旋转等事件 3.远程遥控事件:蓝牙线控,耳机线控等 触摸事件 响应者对象 只有继承了UIResponder得对象才能接收并处理事件 常见类有:U ...
- IOS笔记049-UITabBarController
1.简单实现 效果:在视图底部显示一个工具栏 代码实现 // 创建窗口 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScre ...
- Python+Selenium基础篇之3-打开和关闭IE/Chrome浏览器
前面文章介绍了,如何调用webdriver接口方法来打开和关闭Firefox浏览器,本文介绍如何打开IE和Chrome浏览器.web项目,需要做兼容性测试,最重要的是浏览器兼容性测试.如果只考虑win ...