[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 ...
随机推荐
- 数据库事务的属性-ACID和隔离级别
1.数据库事务的属性-ACID(四个英文单词的首写字母): 1)原子性(Atomicity) 所谓原子性就是将一组操作作为一个操作单元,是原子操作,即要么全部执行,要么全部不执行. 2)一致性(Con ...
- [Java]Servlet&JSP
在这里学习Servlet和JSP >> Servlet&JSP的那些事儿 >> servlet [书籍] 孙鑫的<Servlet/JSP深入详解:基于Tomcat ...
- CocoaPods第三方库管理工具
http://code4app.com/article/cocoapods-install-usage
- 使用 IntraWeb (1) - 先测试如何部署为 Asp.Net 的应用
IntraWeb 14 可以部署为 Asp.Net 的应用程序, 需要 NET Framework 4.5 和 ASP.NET MVC 4 或之上版本的支持; 这下, 只能用虚拟主机的朋友有福了! 我 ...
- SPI SWD Protocol Implement
//================================================================================= // ARM SWD Mode ...
- Android内存机制分析——堆和栈
昨天用Gallery做了一个图片浏览选择开机画面的功能,当我加载的图片多了就出现OOM问题.以前也出现过这个问题,那时候并没有深究.这次打算好好分析一下Android的内存机制. 因为我以前是做VC+ ...
- RDMA over TCP的协议栈工作过程浅析
http://blog.chinaunix.net/uid-140205-id-2849342.html
- linux 内核升级 转
inux 内核升级 2011-03-25 23:13:28 分类: LINUX 因要测试一些软件,需要2.6.30以上的内核,安装好CentOS 5.5,内核是2.6.18-194.el5.这次的升级 ...
- delphi 实现Ribbon风格的窗体
随着office2007的兴起,微软让我们看到了Ribbon风格的窗体,现在很多软件也都开始使用Ribbon风格.那么我们如果要自己开发,应当怎么做呢?本文就是为大家解开这个疑团的. 首先,Delph ...
- Quartz 定时任务设置某个时间区间每隔一定时间触发的cron表达式
原文:https://blog.csdn.net/yansong_8686/article/details/46991189 Cron表达式 Quartz使用类似于Linux下的Cron表达式定义时间 ...