Skip to content

Commit c8fd5ed

Browse files
committed
backtracking
1 parent 7510357 commit c8fd5ed

File tree

4 files changed

+233
-1
lines changed

4 files changed

+233
-1
lines changed

.vscode/launch.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
// 使用 IntelliSense 了解相关属性。
3+
// 悬停以查看现有属性的描述。
4+
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Current File (Integrated Terminal)",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal"
13+
},
14+
{
15+
"name": "Python: Remote Attach",
16+
"type": "python",
17+
"request": "attach",
18+
"port": 5678,
19+
"host": "localhost",
20+
"pathMappings": [
21+
{
22+
"localRoot": "${workspaceFolder}",
23+
"remoteRoot": "."
24+
}
25+
]
26+
},
27+
{
28+
"name": "Python: Module",
29+
"type": "python",
30+
"request": "launch",
31+
"module": "enter-your-module-name-here",
32+
"console": "integratedTerminal"
33+
},
34+
{
35+
"name": "Python: Django",
36+
"type": "python",
37+
"request": "launch",
38+
"program": "${workspaceFolder}/manage.py",
39+
"console": "integratedTerminal",
40+
"args": [
41+
"runserver",
42+
"--noreload",
43+
"--nothreading"
44+
],
45+
"django": true
46+
},
47+
{
48+
"name": "Python: Flask",
49+
"type": "python",
50+
"request": "launch",
51+
"module": "flask",
52+
"env": {
53+
"FLASK_APP": "app.py"
54+
},
55+
"args": [
56+
"run",
57+
"--no-debugger",
58+
"--no-reload"
59+
],
60+
"jinja": true
61+
},
62+
{
63+
"name": "Python: Current File (External Terminal)",
64+
"type": "python",
65+
"request": "launch",
66+
"program": "${file}",
67+
"console": "externalTerminal"
68+
}
69+
]
70+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#
2+
# @lc app=leetcode.cn id=116 lang=python
3+
#
4+
# [116] 填充每个节点的下一个右侧节点指针
5+
#
6+
# https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/description/
7+
#
8+
# algorithms
9+
# Medium (48.22%)
10+
# Likes: 94
11+
# Dislikes: 0
12+
# Total Accepted: 15.2K
13+
# Total Submissions: 31.4K
14+
# Testcase Example: '{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","left":null,"next":null,"right":null,"val":5},"val":2},"next":null,"right":{"$id":"5","left":{"$id":"6","left":null,"next":null,"right":null,"val":6},"next":null,"right":{"$id":"7","left":null,"next":null,"right":null,"val":7},"val":3},"val":1}'
15+
#
16+
# 给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
17+
#
18+
# struct Node {
19+
# ⁠ int val;
20+
# ⁠ Node *left;
21+
# ⁠ Node *right;
22+
# ⁠ Node *next;
23+
# }
24+
#
25+
# 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
26+
#
27+
# 初始状态下,所有 next 指针都被设置为 NULL。
28+
#
29+
#
30+
#
31+
# 示例:
32+
#
33+
#
34+
#
35+
#
36+
# 输入:{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","left":null,"next":null,"right":null,"val":5},"val":2},"next":null,"right":{"$id":"5","left":{"$id":"6","left":null,"next":null,"right":null,"val":6},"next":null,"right":{"$id":"7","left":null,"next":null,"right":null,"val":7},"val":3},"val":1}
37+
#
38+
#
39+
# 输出:{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":{"$id":"4","left":null,"next":{"$id":"5","left":null,"next":{"$id":"6","left":null,"next":null,"right":null,"val":7},"right":null,"val":6},"right":null,"val":5},"right":null,"val":4},"next":{"$id":"7","left":{"$ref":"5"},"next":null,"right":{"$ref":"6"},"val":3},"right":{"$ref":"4"},"val":2},"next":null,"right":{"$ref":"7"},"val":1}
40+
#
41+
# 解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。
42+
#
43+
#
44+
#
45+
#
46+
# 提示:
47+
#
48+
#
49+
# 你只能使用常量级额外空间。
50+
# 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
51+
#
52+
#
53+
#
54+
55+
# @lc code=start
56+
"""
57+
# Definition for a Node.
58+
class Node(object):
59+
def __init__(self, val, left, right, next):
60+
self.val = val
61+
self.left = left
62+
self.right = right
63+
self.next = next
64+
"""
65+
class Solution(object):
66+
def connect(self, root):
67+
"""
68+
:type root: Node
69+
:rtype: Node
70+
"""
71+
if not root or not root.left:return root #为None 或者 为叶子节点
72+
root.left.next=root.right
73+
if root.next:
74+
root.right.next=root.next.left
75+
76+
self.connect(root.left)
77+
self.connect(root.right)
78+
#要先处理根节点保证同层次之间已经串起来了才可以进行下一层,先左边还是右边的顺序无要求
79+
return root
80+
# @lc code=end
81+

51.n皇后.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#
2+
# @lc app=leetcode.cn id=51 lang=python
3+
#
4+
# [51] N皇后
5+
#
6+
# https://leetcode-cn.com/problems/n-queens/description/
7+
#
8+
# algorithms
9+
# Hard (66.02%)
10+
# Likes: 245
11+
# Dislikes: 0
12+
# Total Accepted: 16.6K
13+
# Total Submissions: 25K
14+
# Testcase Example: '4'
15+
#
16+
# n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
17+
#
18+
#
19+
#
20+
# 上图为 8 皇后问题的一种解法。
21+
#
22+
# 给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。
23+
#
24+
# 每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
25+
#
26+
# 示例:
27+
#
28+
# 输入: 4
29+
# 输出: [
30+
# ⁠[".Q..", // 解法 1
31+
# ⁠ "...Q",
32+
# ⁠ "Q...",
33+
# ⁠ "..Q."],
34+
#
35+
# ⁠["..Q.", // 解法 2
36+
# ⁠ "Q...",
37+
# ⁠ "...Q",
38+
# ⁠ ".Q.."]
39+
# ]
40+
# 解释: 4 皇后问题存在两个不同的解法。
41+
#
42+
#
43+
#
44+
45+
# @lc code=start
46+
class Solution(object):
47+
def solveNQueens(self, n):
48+
"""
49+
:type n: int
50+
:rtype: List[List[str]]
51+
"""
52+
res=[]
53+
board=[['.' for i in range(n)] for i in range(n)]
54+
col=[True]*n
55+
diag1=[True]*(2*n-1) #左下到右上的许多对角线,从左上到右下从0开始编号
56+
diag2=[True]*(2*n-1) #左上到右下的许多对角线,从左下到右上从0开始编号
57+
58+
def updateBoard(x,y,n,flag):
59+
col[x]=flag
60+
diag1[x+y]=flag
61+
diag2[x-y+n-1]=flag
62+
board[y][x]='Q' if not flag else '.'
63+
def available(x,y):
64+
return col[x] and diag1[x+y] and diag2[x-y+n-1]
65+
def nqueens(y,n):
66+
if y>=n:
67+
temp=[]
68+
for i in range(n):
69+
temp.append(''.join(board[i]))
70+
res.append(temp)
71+
return
72+
for x in range(n): #同一排挨个试
73+
if not available(x,y):
74+
continue
75+
updateBoard(x,y,n,False) #直到出现一个可以填Q的
76+
nqueens(y+1,n) #进行下一行的选取
77+
updateBoard(x,y,n,True) #再调整回来
78+
nqueens(0,n)
79+
return res
80+
# @lc code=end
81+

78.子集.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def subsets(self, nums):
4545
def dfs(self,index,path,nums,res):
4646
res.append(path)
4747
print(path)
48-
for i in xrange(index,len(nums)):
48+
for i in range(index,len(nums)):
4949
self.dfs(i+1,path+[nums[i]],nums,res)
5050

5151
#第二种:列表推导式

0 commit comments

Comments
 (0)