[leetcode]N-Queens II @ Python
原题地址:https://oj.leetcode.com/problems/n-queens-ii/
题意:和N-Queens这道题其实是一样的,只不过这次要求返回的时N皇后的解的个数的问题。
解题思路:上道题使用了递归回溯的解法,这道题我们可以使用非递归回溯来解决,因为如果使用递归回溯来解决,那么代码和上道题几乎一样。在非递归的编程中,比较有技巧性的是如何来进行回溯。
代码:
class Solution:
# @return an integer
def totalNQueens(self, n):
def check(k, j): # check if the kth queen can be put in column j!
for i in range(k):
if board[i]==j or abs(k-i)==abs(board[i]-j):
return False
return True
board=[-1 for i in range(n)]
row=0; col=0; sum=0
while row<n:
while col<n:
if check(row, col):
board[row]=col
col=0
break
else:
col+=1
if board[row]==-1: #如果为真,即为在这一行找不到位置放置皇后
if row==0: #如果在第0行也找不到位置放置皇后了,说明所有的情况已经迭代完毕了,执行break跳出操作。
break
else:
row-=1 #这条语句用来回溯到上一行
col=board[row]+1 #回溯到上一行时,皇后放置的位置要加1,也就是开始试验下一列
board[row]=-1 #然后将这一行的值重置为-1,也就是说要重新寻找皇后的位置了
continue
if row==n-1: #当row==n-1时,说明最后一行的皇后位置也确定了,得到了一个解
sum+=1
col=board[row]+1
board[row]=-1
continue
row+=1
return sum
[leetcode]N-Queens II @ Python的更多相关文章
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- [leetcode]Spiral Matrix II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- [leetcode]Path Sum II @ Python
原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- [Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- leetcode Jump Game II python
@link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...
- leetcode Combination Sum II python
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- linux驱动之LCD(无framebuffer)
<简介> a:什么是液晶 物质一般有三态,固态,气态,和液态.这只是一种比较大致的划分,但是有些物质介于液体和固体之间——液晶.一般固体的分子或原子都由固定的排列方式,但是液晶介于固体和液 ...
- [Android]对BaseAdapter中ViewHolder编写简化(转)
来自博客:http://www.cnblogs.com/tiantianbyconan/p/3642849.html 在Android项目中,经常都会用到ListView这个控件,而相应的Adapte ...
- TVS二极管和稳压二极管的区别
TVS二极管和稳压二极管的区别 TVS管超过它的耐压值后,会瞬间导通短路,反应速度在ns级, 而稳压管是稳压作用的,超过它的稳压值,只要功率不超过它的耐受值,就会稳定在它的稳压值范围内. TVS是瞬态 ...
- JTAG - Debug Cable Driver/Receiver
- C# MD5 32位加密 UTF-8编码
项目开发过程中需要用到MD5加密,最开始的使用使用加密方法: public static string GetMD5(string str) { byte[] b ...
- EF6+Sqlite连接字符串的动态设置
摘要 在winform中应用sqlite和ef,对于sqlite连接字串的设置,大多情况下是不想写死了,你不知道用户会将你的exe程序安装在什么位置,也不知道他的电脑盘符是什么,如果写死了,那么很有可 ...
- .Net Discovery 系列之五--深入浅出.Net实时编译机制(上)
欢迎阅读“.Net Discovery 系列”文章,本文将分上.下两部分为大家讲解.Net JIT方面的知识,敬请雅正. JIT(Just In Time简称JIT)是.Net边运行边编译的一种机制, ...
- Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(4) DateFormat
本章主要介绍DateFormat. DateFormat 介绍 DateFormat 的作用是 格式化并解析“日期/时间”.实际上,它是Date的格式化工具,它能帮助我们格式化Date,进而将Date ...
- iOS webservice 接口使用方法
1.没有参数的webservice 接口,如下: 接口名:获取服务器时间 方法名: getServerTime() 参数:空 返回值:服务器当前时间: yyyy/MM/dd HH:mm:ss 使用方法 ...
- iphone6/6+ 适配心得
1. 文档综述 自iphone6/6+发布,ios屏幕分辨率的种类一下从2种变成了四种.对于以前很多手写UI,并且使用绝对坐标的UI,可能会发生异变,本文主要介绍在纯手写UI条件下,ios应用 ...