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 ...
随机推荐
- OneWire总线的Arduino库函数
OneWire总线基本点 One-wire总线是DALLAS公司研制开发的一种协议,采用单根信号线,既传输时钟,又传输数据而且数据传输是双向的.它具有节省I/O 口线资源.结构简单.成本低廉.便于总线 ...
- 搭建zabbix+grafana监控
编写一件安装脚本 #!/bin/sh echo "\033[32;1m脚本作者:fage\033[0m" #sleep 10 zabbix_version=4.0.2 zabbix ...
- Netty之网络编程数据编码
一.概况 我们在进行网络编程中会把各种数据转换为byte数据以便能在网络上传输,最常见的网络字节序--Little-Endian和Big-Endian,也让好多初进网络编程的新手摸不着头脑,还有按位或 ...
- 关于.netMVC 出现@ViewBag 出现错误(波浪红线)的解决方法
解决vs2015.vs2013解决mvc5 viewbag问题 1.关闭vs2015或者vs2013 打开我的电脑或者文件夹 2.打开我的电脑 在地址栏输入 %UserProfile%\AppData ...
- 爬虫之Selenium
简介 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如 ...
- day55 Pyhton 前端Jquery07
昨日回顾: 表单,点击submit提交以后,服务端受到信息 import socket import pymysql from urllib.parse import unquote def run( ...
- spring boot:配置shardingsphere(sharding jdbc)使用druid数据源(druid 1.1.23 / sharding-jdbc 4.1.1 / mybatis / spring boot 2.3.3)
一,为什么要使用druid数据源? 1,druid的优点 Druid是阿里巴巴开发的号称为监控而生的数据库连接池 它的优点包括: 可以监控数据库访问性能 SQL执行日志 SQL防火墙 但spring ...
- PHP SPL标准库-接口
PHP SPL标准库有一下接口: Countable OuterIterator RecursiveIterator SeekableIterator SplObserver SplSubject A ...
- js实现无缝连接轮播图(二)实现自定义属性,根据banner图片的数量动态生成小圆点
<!-- 这个animate.js 必须写到 index.js的上面引入 --><script src="js/animate.js"></scrip ...
- c++ qsort的使用
c++ qsort的使用 qsort函数定义在头文件algorithm中,使用时需要include该头文件 void qsort (void* base, size_t num, size_t siz ...