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

  1. LeetCode OJ Basic Calculator II

    Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: strin ...

  2. LeetCode OJ 47. Permutations II

    题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...

  3. LeetCode OJ:Permutations II(排列II)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. LeetCode OJ:Subsets II(子集II)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  5. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  6. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

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

  8. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  9. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  10. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

随机推荐

  1. 数据结构-模式匹配串算法(KMP)

    #include<cstdio> #include<iostream> #include<string> #include<cstring> #incl ...

  2. 2017 United Kingdom and Ireland Programming(Gym - 101606)

    题目很水.睡过了迟到了一个小时,到达战场一看,俩队友AC五个了.. 就只贴我补的几个吧. B - Breaking Biscuits Gym - 101606B 旋转卡壳模板题.然后敲错了. 代码是另 ...

  3. readhat7.0 bond配置

    Bonding的模式一共有7种: 1.mode=0(balance-rr)(平衡抡循环策略) 概念:链路负载均衡,增加带宽,支持容错,一条链路故障会自动切换正常链路.交换机需要配置聚合口,思科叫por ...

  4. mysql--timestamp加减

    利用timestamp()对timestamp类型进行秒加减操作: 1.加10秒: 2.减10秒:

  5. 2.使用vue ui命令快速构建应用

    直接在web端新建应用 C:\Users\Hugo> vue ui

  6. laravel5.2总结--响应

      1 基本响应 1.1 返回一个字符串,指定的字符串会被框架自动转换成 HTTP 响应. Route::get('/', function () { return 'Hello World'; }) ...

  7. C#入门篇6-1:字符串操作 char常用的函数

    //char 字符的常见操作 public static void FChar() { //判定字符的类别 char ch1 = 'a';//使用小引号 bool bl = true; bl = ch ...

  8. 设计模式之第5章-解释器模式(Java实现)

    设计模式之第5章-解释器模式(Java实现) “开个商店好麻烦,做个收单的系统,发现类的方法好多.”“真是的,不就是简单的四则运算,这都不会!”你说你会啊.来来来,你把以下的方法用代码写出来: a+b ...

  9. 使用Fiddler对Android应用进行抓包

    1.  打开Fiddler软件,效果图如下: 2. 首先,确保安装 Fiddler 的电脑和你的手机在同一局域网内,因为Fiddler只是一个代理,需要将手机的代理指向 PC 机,不能互相访问是不行的 ...

  10. 移动弱网测试方案Network Emulator for Windows Toolkit

    移动app在测试时,有时需要考虑弱网的情形下,app的表现,那么怎么营造这样子的环境呢? 一.首先需要控制网络,有两种方式其一使用网络损伤仪进行,其二采用软件方式.硬件采购费用太贵,因此使用win平台 ...