Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

  1. class Solution {
  2. public:
  3. int totalNQueens(int n) {
  4. vector<int> queen(n,);
  5. for(int i = ; i < n; ++ i) queen[i] = i;
  6. int cnt = ;
  7. do{
  8. bool flag = false;
  9. for(int i = ; i < n && !flag; ++ i){
  10. for(int j = ; j < i && !flag ; ++ j){
  11. if(abs(i-j) == abs(queen[i]-queen[j])) flag=true;
  12. }
  13. }
  14. if(!flag) cnt++;
  15. }while(next_permutation(queen.begin(),queen.end()));
  16. return cnt;
  17. }
  18. };

Leetcode N-Queens II的更多相关文章

  1. [Leetcode] n queens ii n皇后问题

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  2. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  4. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  5. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  6. LeetCode:子集 II【90】

    LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...

  7. [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用

    [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...

  8. Leetcode | N-Queens I & II

    N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...

  9. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  10. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

随机推荐

  1. avalonjs

    var vm = avalon.define({ $id: "login", username: "", password: "", mes ...

  2. centos 7.0 nginx 1.7.9成功安装过程

    centos 7.0根目录 的目录构成 [root@localhost /]# lsbin dev home lib64 mnt proc run srv tmp varboot etc lib me ...

  3. safari浏览器在window下 打开控制台

    有时候需要在window下测试safari浏览器的兼容性 然后需要打开错误控制台 以下是完整打开的图文教程 1.显示菜单栏 2.打开偏好设置 3.然后切换到高级标签 勾选 在菜单栏显示开发菜单 4.打 ...

  4. Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)

    功能说明 输入文件路径,在浏览器输出文件预览信息,经测试极速(Chrome).IE9.Firefox通过 分类文件及代码说明  DemoFiles 存放可测试文件 Default.aspx  启动页 ...

  5. fzoj1314 You are my brother

    题目描述 Little A gets to know a new friend, Little B, recently. One day, they realize that they are fam ...

  6. PHP异常处理函数set_exception_handler()的用法

    定义和用法 set_exception_handler() 函数设置用户自定义的异常处理函数. 该函数用于创建运行时期间的用户自己的异常处理方法. 该函数会返回旧的异常处理程序,若失败,则返回 nul ...

  7. Swift3.0P1 语法指南——类和结构体

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  8. struts2 用if标签判断字符串包含

    String testStr = "用来判断是否包含的字符串"; <s:property value="testStr"/> <s:if te ...

  9. BZOJ 4724: [POI2017]Podzielno

    Description 由\([0,B-1]\)的数字构造一个 \(B\) 进制数字,使得他是 \(B-1\) 的倍数. Sol 贪心+二分. 首先 \(X\) 是 \(B-1\) 的倍数,那么有 \ ...

  10. json死循环问题

    20.JSON死循环问题: 向前台发送的数据: 出现此类问题主要是由于在所传数据中有包含关系,比如ElementGroup中有Element,Element中又有ElementGroup,此时就会出现 ...