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. dfs 的全排列

    #include <iostream> #include <algorithm> #include <cstdio> #include <string> ...

  2. 扩展程序 - Google Chrome

    Adblock Plus 3.0.3 Adblock Plus 已被超过 1 亿台设备使用,是世界上最受欢迎的广告拦截软件. ID:cfhdojbkjhnklbpkdaibdccddilifddb 查 ...

  3. (HTML)A标签伪元素选择器的继承关系

    ①如果a:link{}也存在,那么不管a{}放到哪里,a{}和a:link{}冲突的属性都会采用a:link{}的,不冲突的属性若存在a{}中,会被a:link{}. a:visited{} .a:h ...

  4. webpack + babel + vue 环境设置

    npm i webpack --save-dev npm install style-loader css-loader url-loader babel-loader sass-loader fil ...

  5. Java面试——多线程面试题总结

    )两者都在等待对方所持有但是双方都不释放的锁,这时便会一直阻塞形成死锁. //存放两个资源等待被使用 public class Resource { public static Object obj1 ...

  6. OpenCV学习笔记(六) 滤波器 形态学操作(腐蚀、膨胀等)

    转自:OpenCV 教程 另附:计算机视觉:算法与应用(2012),Learning OpenCV(2009) 平滑图像:滤波器 平滑 也称 模糊, 是一项简单且使用频率很高的图像处理方法.平滑处理的 ...

  7. jq阻止ajax进行多次提交

    在函数定义全局变量..var Stch=falseif (Stch==true){alert('请不要重新提交');}else{Stch=true;$.ajax({type:"POST&qu ...

  8. 56、使用android studio(v1.3.*)修改包名 (rename package name)

    一.修改包名 ①选中目录,开始构造 在弹窗中选中Rename directory 在弹窗中选中Rename package 填写新的包名,点击Refactor 如果有警告,不用管它,直接点击Do Re ...

  9. IOS笔记050-事件处理

    IOS事件处理 1.触摸事件 2.加速器事件:重力感应,旋转等事件 3.远程遥控事件:蓝牙线控,耳机线控等 触摸事件 响应者对象 只有继承了UIResponder得对象才能接收并处理事件 常见类有:U ...

  10. linux环境搭建系列之maven

    前提: jdk1.7 Linux centOS 64位 安装包从官网获取地址:http://maven.apache.org/download.cgi Jdk1.7对应apache-maven-3.3 ...