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. 2018年江西理工大学C语言程序设计竞赛(高级组) 三角平方数

    题目描述 三角数:形如图a,圆点摆放成等边三角形的数字,则为三角数. (图a) 平方数:形如图b,小方块摆放成正方形的数字,则为平方数. (图b) 那么如果一个数字既是三角形数又是平方数,则称为三角平 ...

  2. Kali 安装VMtools(最新)

    老方法安装的VMtools不能进行主宿切换,下面是kali最新版安装VMtools的方法 一.换国内源&更新源 参考 Kali 2017更新源 二.安装VMtools apt-get inst ...

  3. Java-读取txt生成excel

    本段代码的目的是从txt文本中读取相应格式的数据,然后写入到对应格式的excel文档中 在敲本段代码的时候,也学习了一些其它知识点,如下: 1.byte[] b_charset= String.get ...

  4. datagrid的右键菜单

    1. 2.右键菜单,主要是用onRowContextMenu:function(e,index,row){}方法来实现 onRowContextMenu:function(e,index,row){ ...

  5. leetcode 【 Find Minimum in Rotated Sorted Array 】python 实现

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  6. Python中的Json模块dumps、loads、dump、load函数介绍

    Json模块dumps.loads.dump.load函数介绍 1.json.dumps() json.dumps() 用于将dict类型的数据转成str,因为如果直接将dict类型的数据写入json ...

  7. js跨域post请求

    function funPostBack(srvMethod){ /* var contentNR=$(document.getElementById("reportFrame") ...

  8. astyle使用基础教程

    astyle使用基础教程 转自: http://babybandf.blog.163.com/blog/static/61993532010112205811797/ astyle是一个我自己常用的开 ...

  9. IE IE8 iframe的onload方法分析 IE浏览器onload事件失效

    判断iframe是否加载完成的完美方法 IE 支持 iframe 的 onload 事件,不过是隐形的,需要通过 attachEvent 来注册. 第二种方法比第一种方法更完美(采用readystat ...

  10. 如何将RobotFramework中case的执行结果上传到TestLink中。

    公司的需求是: 用RobotFrameworjk框架执行case,用Testlink管理case和测试任务.需要持续统计每个版本的测试结果. 我觉得用Jenkins+Robot也行,Testlink+ ...