Leetcode回溯相关题目Python实现
1、46题,全排列
https://leetcode-cn.com/problems/permutations/
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
n = len(nums)
results = []
def backtrack(first = 0):
if first == n:
results.append(nums[:])
return
for i in range(first, n):
nums[first], nums[i] = nums[i], nums[first]
backtrack(first + 1)
nums[first], nums[i] = nums[i], nums[first]
backtrack()
return results
2、47题,全排列二
https://leetcode-cn.com/problems/permutations-ii/
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
n = len(nums)
results = []
def backtrack(first = 0):
if first == n:
results.append(nums[:])
return
s = set()
for i in range(first, n):
if nums[i] in s:
continue
s.add(nums[i])
nums[first], nums[i] = nums[i], nums[first]
backtrack(first + 1)
nums[first], nums[i] = nums[i], nums[first]
backtrack()
return results
3、51题,N皇后
https://leetcode-cn.com/problems/n-queens/
class Solution(object):
def solveNQueens(self, n):
"""
:type n: int
:rtype: List[List[str]]
"""
results = []
result = ["." * n] * n
l = set()
c = set()
x = set()
y = set() def backtrack(first=0):
if first == n:
results.append(result[:])
return
for i in range(n):
if i in l or first in c or i - first in x or i + first in y:
continue
l.add(i), c.add(first), x.add(i - first), y.add(i + first)
result[i] = first * "." + "Q" + (n - first - 1) * "."
backtrack(first + 1)
l.remove(i), c.remove(first), x.remove(i - first), y.remove(i + first)
result[i] = n * "."
backtrack()
return results
4、52题,N皇后二
https://leetcode-cn.com/problems/n-queens-ii/
class Solution(object):
def totalNQueens(self, n):
"""
:type n: int
:rtype: int
"""
self.results = 0
l = set()
c = set()
x = set()
y = set() def backtrack(first=0):
if first == n:
self.results += 1
return
for i in range(n):
if i in l or first in c or i - first in x or i + first in y:
continue
l.add(i), c.add(first), x.add(i - first), y.add(i + first)
backtrack(first + 1)
l.remove(i), c.remove(first), x.remove(i - first), y.remove(i + first)
backtrack()
return self.results
Leetcode回溯相关题目Python实现的更多相关文章
- leetcode tree相关题目总结
leetcode tree相关题目小结 所使用的方法不外乎递归,DFS,BFS. 1. 题100 Same Tree Given two binary trees, write a function ...
- [LeetCode] [链表] 相关题目总结
刷完了LeetCode链表相关的经典题目,总结一下用到的技巧: 技巧 哑节点--哑节点可以将很多特殊case(比如:NULL或者单节点问题)转化为一般case进行统一处理,这样代码实现更加简洁,优雅 ...
- [LeetCode] 二叉树相关题目(不完全)
最近在做LeetCode上面有关二叉树的题目,这篇博客仅用来记录这些题目的代码. 二叉树的题目,一般都是利用递归来解决的,因此这一类题目对理解递归很有帮助. 1.Symmetric Tree(http ...
- LeetCode - 排列相关题目
1.获取全排列 https://leetcode.com/problems/permutations/submissions/ 按字典序输出: 这里用的是vector<int>,不是引用. ...
- [LeetCode] 链表反转相关题目
暂时接触到LeetCode上与链表反转相关的题目一共有3道,在这篇博文里面总结一下.首先要讲一下我一开始思考的误区:链表的反转,不是改变节点的位置,而是改变每一个节点next指针的指向. 下面直接看看 ...
- LeetCode: Palindrome 回文相关题目
LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...
- 【数据结构】Hash表简介及leetcode两数之和python实现
文章目录 Hash表简介 基本思想 建立步骤 问题 Hash表实现 Hash函数构造 冲突处理方法 leetcode两数之和python实现 题目描述 基于Hash思想的实现 Hash表简介 基本思想 ...
- LeetCode初级算法的Python实现--链表
LeetCode初级算法的Python实现--链表 之前没有接触过Python编写的链表,所以这里记录一下思路.这里前面的代码是和leetcode中的一样,因为做题需要调用,所以下面会给出. 首先定义 ...
- leetcode - 位运算题目汇总(下)
接上文leetcode - 位运算题目汇总(上),继续来切leetcode中Bit Manipulation下的题目. Bitwise AND of Numbers Range 给出一个范围,[m, ...
随机推荐
- 有几个水洼(DFS)
#include <iostream> #include<cstdio> using namespace std; #define maxn 105 char field[ma ...
- windows下查看rabbitmq服务是否启动
1.命令行进入rabbitmq的安装目录下: 如下图1步骤 2.输入命令 rabbitmqctl1 status 如下图2步骤 3.有时会提示报错,如步骤3 解决办法: 我的电脑 ==> 右键 ...
- dubbo使用Spring配置暴露服务和使用Spring配置引用远程服务
提供者: <!-- 1.指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) --> <dubbo:application name="user-servic ...
- Linux mint OS
Linux mint OS Ctrl+Alt left or right : # Change workspace Goldendict + Goldendict-wordnet # Dict for ...
- 14 微服务电商【黑马乐优商城】:day04-项目搭建(二)
本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...
- (vshadow)Volume Shadow在渗透测试中的利用
本文根据嘶吼学习总结出文中几种方式Vshadow包含在window SDK中,由微软签名. Vshadow包括执行脚本和调用支持卷影快照管理的命令的功能,这些功能可能会被滥用于特权级的防御规避,权限持 ...
- RDD(十)——案例实操
需求: 数据结构:时间戳,省份,城市,用户,广告,中间字段使用空格分割. 样本如下: 1516609143867 6 7 64 16 1516609143869 9 4 75 18 151660914 ...
- swift中的坑
1.NSClassFromString //获取工程名称 let group = Bundle.main.infoDictionary let fileName = group?[kCFBundleE ...
- Work Scheduling(带反悔的贪心)
https://www.luogu.org/problem/P2949 题目描述 Farmer John has so very many jobs to do! In order to run th ...
- LGOJ3327 【SDOI2015】约数个数和
又是一道卡常好题 坑掉我的 \(define \space int \space long \space long\) 感觉出题人并没有获得什么快乐-- Description link 题意概述: ...