leetcode N-QueensII
题目和上一题一样,就是要求输出有多少种结果。最直接的就是,只要在上一题的代码return ans.size();就可以了。果然也是AC了。
然后我翻看了几种别人写的,暂时还没有找到复杂度可以比上一题降低多少的。
可以加一个全局变量。以前都没有想到给solution类加全局变量。满足条件的时候全局变量加一。随后返回全局变量就好了。
class Solution {
int ansT = ;
public:
int totalNQueens(int n) { vector<vector<string> > ans;
//ans.clear();
int perm[n];
//memset(perm, 0, sizeof(perm));
solve50(perm, , n, ans);
return ansT;
}
void solve50(int perm[], int row, int n, vector<vector<string> > &ans)
{
if (row == n) // 因为row从0开始,说明已经有0到n-1总共n个符合了
{
ansT++;
return;
}
else
{
for (int col = ; col < n; ++col)//对与第row行的每一个列,进行判断是否符合
{
bool flag = true;
for(int i = ; i < row; ++i)//对于第row行的每一个列要与之前的每行锁存的王后判断是否冲突
{
if (col == perm[i] || col - perm[i] == row - i || col - perm[i] == i - row)
{// 当前列等于之前的列,或者当前的点和之前的点的斜率为正负1时,为false,否则true进行判断下一行
flag = false;
}
}
if (flag)//没有冲突,记录当前列数,进入下一行的递归选择
{
perm[row] = col;
solve50(perm, row + , n, ans);
}
}
}
}
};
有空的时候看看这里介绍的据说比较高效一些的方法。
leetcode N-QueensII的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- SDUT OJ 2463 学校password你必须学会科学计划
#include<iostream> #include<string.h> #include<stdio.h> #define N 10010 #define M ...
- Ajax请求访问action推断文件是否存在
action措辞: public ActionForward fileIsExsit(ActionMapping mapping, ActionForm form, HttpServletReques ...
- 关于TCP/IP协议栈(转)
1. TCP/IP协议栈 与OSI参考模型不同,TCP/IP协议栈共有4层,其中网络接口层对应OSI中的物理层和数据链路层,应用层对应OSI中的应用层.表示层和会话层. 在网络接口层的主要协议有:AR ...
- quick-cocos2d-x游戏开发【5】——创建菜单
一个菜单是游戏中的一个基本要素,quick在里面menuItem有两个包.一个是图片菜单.一个文本菜单. 一个.图片菜单ui.newImageMenuItem(params) 參数: image: 正 ...
- 阿里云ECSserver部署django
highlight=uwsgi%20django">參考 server安装的是Centos 系统. uwsgi是使用pip安装的. nginx是使用yum install nginx安 ...
- HDU 3853 LOOPS 可能性dp(水
在拐~ #include <stdio.h> #include <cstring> #include <iostream> #include <map> ...
- Model绑定
Model绑定 在前面的几篇文章中我们都是采用在URI中元数据类型进行传参,实际上ASP.NET Web API也提供了对URI进行复杂参数的绑定方式--Model绑定.这里的Model可以简单的理解 ...
- SQLServer 2012异常问题(二)--由安装介质引发性能问题
原文:SQLServer 2012异常问题(二)--由安装介质引发性能问题 问题描述:生产环境一个数据库从SQLSERVER 2008 R2升级到SQLSERVER 2012 ,同时更换硬件,但迁移后 ...
- Windows下一个MySQL有些错误的解决方法
1.无论是什么提示.我们有一个直接看错误日志.由于它描述了最具体描述错误日志. 于MySQL安装文件夹中找到 my.ini简介 看日志保存路径 2. 我的错误是[ERROR] Fatal error: ...
- UVa 10285 - Longest Run on a Snowboard
称号:给你一个二维矩阵,找到一个点.每一个可以移动到的位置相邻的上下,求最长单调路径. 分析:贪婪,dp.搜索. 这个问题是一个小样本,我们该怎么办. 这里使用贪心算法: 首先.将全部点依照权值排序( ...