leetcode 98:n-queens-ii
题目描述

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

输出
class Solution {
public:
/**
*
* @param n int整型
* @return int整型
*/
void f(int &cnt,int a[],int n,int level){
if (level ==n) {cnt++;return;}
for (int i=0;i<n;i++){
int j=0;
for (;j<level;j++){
if (a[j]==i || level -j ==i-a[j] || j-level==i-a[j]) break;
}
if (j==level){
a[level]=i;
f(cnt,a,n,level+1);
}
}
}
int totalNQueens(int n)
{
int a[n];
int cnt=0;
f(cnt,a,n,0);
return cnt;
}
};
leetcode 98:n-queens-ii的更多相关文章
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- [Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [Leetcode][Python]52: N-Queens II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
随机推荐
- 【CSP2019-J】游记
看我朋友们的博客里面都写了游记,我也来凑个热闹(雾) day1# 介于是\(CSP-J\),我们是比赛当天走的,上午卡点到.一路上不允许玩游戏,于是就在路上看了一路的鬼畜视频,然后看了看对拍的板子(然 ...
- 深入浅出学Java-HashMap
一.概要 HashMap在JDK1.8之前的实现方式 数组+链表,但是在JDK1.8后对HashMap进行了底层优化,改为了由 数组+链表+红黑树实现,主要的目的是提高查找效率. 如下图所示: JDK ...
- css做模糊处理
-webkit-filter: blur(9px); filter: blur(9px);
- (转载)Quartus II中FPGA的管脚分配保存方法(Quartus II)
一.摘要 将Quartus II中FPGA管脚的分配及保存方法做一个汇总. 二.管脚分配方法 FPGA 的管脚分配,除了在QII软件中,选择"Assignments ->Pin&quo ...
- 多测师讲解自动化测试 _pybot.bat批处理脚本_高级讲师肖sir
- 程序3-6 WERTYU
把手放在键盘上时,稍不注意就会往右错一 位.这样,输入Q会变成输入W,输入J会变成输 入K等.键盘如图3-2所示. 输入一个错位后敲出的字符串(所有字母均 大写),输出打字员本来想打出的句子.输入保 ...
- Spring Aop 详解一
Aop 是一个编程思想,最初是一个理论,最后落地成了很多的技术实现. 我们写一个系统,都希望尽量少写点儿重复的东西.而很多时候呢,又不得不写一些重复的东西.比如访问某些方法的权限,执行某些方法性能的日 ...
- centos8安装RabbitMQ
一.安装erlang # 添加仓库 curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh ...
- Topsis优劣解距离法 mlx代码
请参考https://blog.csdn.net/qq_36384657/article/details/98188769 mlx代码 topsis 优劣解距离法 参数说明: 分数.获奖次数.价值等 ...
- unittest学习
unittest的四大特点 TestCase:测试用例.所有的用例都是直接继承与UnitTest.TestCase类. TestFixture:测试固件.setUp和tearDown分别作为前置条件和 ...