52. N-Queens II Leetcode Python
来源:互联网 发布:记工时的软件 编辑:程序博客网 时间:2024/06/05 23:08
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
this problem is slightly different from prior one. Here we just need to update with counting number.
we can define a self.count in the __init__ so that we can update it every time when the depth==n.
the time complexity is still exponential
and the space complexity is O(n)
Here is the code.
class Solution: # @return an integer def __init__(self): self.count=0 def totalNQueens(self, n): def check(k,j): for i in range(k): if board[i]==j or abs(k-i)==abs(board[i]-j): return False return True def dfs(depth): if depth==n: self.count+=1 return for i in range(n): if check(depth,i): board[depth]=i s='.'*n dfs(depth+1) board=[-1 for i in range(n)] dfs(0) return self.count
0 0
- 【Leetcode】【python】N-Queens/N-Queens II
- 52. N-Queens II Leetcode Python
- 52. N-Queens II Leetcode Python
- LeetCode 52. N-Queens II
- LeetCode --- 52. N-Queens II
- LeetCode 52.N-Queens II
- [Leetcode] 52. N-Queens II
- [leetcode] 52.N-Queens II
- [leetcode] 52. N-Queens II
- LeetCode 52. N-Queens II
- leetcode 52. N-Queens II
- LeetCode 52. N-Queens II
- LeetCode 52. N-Queens II
- leetcode.52. N-Queens II
- LeetCode 52. N-Queens II
- [LEETCODE]52. N-Queens II
- [LeetCode] 52. N-Queens II
- leetcode 52. N-Queens II
- VS2010界面主题更换全过程
- div section和article区别
- IOS第三方数据库--FMDB
- Android 闹钟 开发过程记录(一)
- 16道嵌入式C语言面试题(经典)
- 52. N-Queens II Leetcode Python
- solr(一)配置文件详解与整合zookeeper集群
- 计算时间复杂度 -- 写程序需瞻前顾后
- 高精度计算问题中使用double失败的原因
- Xcode 升級後,常常遇到的遇到的警告、錯誤,解決方法
- A. A and B and Chess
- 32位/64位机上常用数据类型字节数(C语言)
- Object-C中对时间的处理
- 【学习笔记】"ListView滑动删除 ,仿腾讯QQ"(三)