Skip to content

Commit 928806f

Browse files
authored
Add files via upload
1 parent 212dba3 commit 928806f

20 files changed

+401
-0
lines changed

leetcode/007.反转整数.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 直接判断正负两种情况
2+
class Solution(object):
3+
def reverse(self, x):
4+
x = -int(str(x)[::-1][:-1]) if x<0 else int(str(x)[::-1])
5+
return x if abs(x)<0x7FFFFFFF else 0
6+
# return x if -2**31<=x<=2**31-1 else 0
7+
8+
# 标记正负两种情况
9+
class Solution(object):
10+
def reverse(self, x):
11+
sign = 1 if x>0 else -1
12+
x = sign * int(str(abs(x))[::-1])
13+
return x if abs(x)<0x7FFFFFFF else 0

leetcode/669.修剪二叉搜索树.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 从根节点开始,若根节点在给定范围左侧,那么他和他的左子树完全裁剪;
2+
# 若根节点在给定范围右侧,那么他和他的右子树完全裁剪;
3+
# 若根节点在给定范围内,不用裁剪,按照递归分别推出他左右子树的裁剪结果。
4+
class Solution(object):
5+
def trimBST(self, root, L, R):
6+
if not root:
7+
return
8+
elif root.val < L:
9+
return self.trimBST(root.right, L, R)
10+
elif root.val > R:
11+
return self.trimBST(root.left, L, R)
12+
else:
13+
root.left = self.trimBST(root.left, L, R)
14+
root.right = self.trimBST(root.right, L, R)
15+
return root
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 由题可知,根结点是最小的结点,则左右子树中节点值不等于根节点值的所有节点中,值最小的即为第二小的值
2+
class Solution(object):
3+
def findSecondMinimumValue(self, root):
4+
if not root or not root.left:
5+
return -1
6+
return self.find(root, root.val)
7+
8+
def find(self, root, minVal):
9+
if not root.left:
10+
return -1 if root.val == minVal else root.val
11+
leftMin = self.find(root.left, minVal)
12+
rightMin = self.find(root.right, minVal)
13+
if leftMin == -1 or rightMin == -1:
14+
return max(leftMin, rightMin)
15+
return min(leftMin, rightMin)

leetcode/684.冗余连接.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 寻根,当一条边让数组内构成循环,则删除该边
2+
class Solution(object):
3+
def findRedundantConnection(self, edges):
4+
tree = [-1] * (len(edges) + 1)
5+
for edge in edges:
6+
a = self.findRoot(edge[0], tree)
7+
b = self.findRoot(edge[1], tree)
8+
if a != b:
9+
tree[a] = b
10+
else:
11+
return edge
12+
13+
def findRoot(self, x, tree):
14+
if tree[x] == -1:
15+
return x
16+
root = self.findRoot(tree[x], tree)
17+
tree[x] = root
18+
return root

leetcode/685.冗余连接II.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution(object):
2+
def findRedundantDirectedConnection(self, edges):
3+
def find(x, tree):
4+
if tree[x] == -1:
5+
return x
6+
root = find(tree[x], tree)
7+
tree[x] = root
8+
return root
9+
10+
# 用 count 来计数不合法的边
11+
count = 0
12+
res = []
13+
tree = [-1] * (len(edges)+1)
14+
# 第一轮查找不合法的边(正序)
15+
for parent, child in edges:
16+
# child已经有parent 或 child的parent的parent(或者一直往上找)就是 child 本身
17+
if tree[child] != -1 or find(parent, tree) == child:
18+
res = [parent, child]
19+
count += 1
20+
else:
21+
tree[child] = parent
22+
# 如果只有一条不合法的边,直接返回
23+
if count == 1:
24+
return res
25+
26+
# 要求返回最后一条边,重置 tree 并开始第二轮查找(逆序)
27+
tree = [-1] * (len(edges)+1)
28+
for parent, child in edges[::-1]:
29+
if tree[child] != -1 or find(parent, tree) == child:
30+
return [parent, child]
31+
else:
32+
tree[child] = parent
33+
return res

leetcode/687.最长同值路径.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
3+
def __init__(self):
4+
self.maxLen = 0
5+
6+
def longestUnivaluePath(self, root):
7+
if not root:
8+
return 0
9+
self.getMaxLen(root, root.val)
10+
return self.maxLen
11+
12+
# 由左右最长同值路径相加
13+
def getMaxLen(self, root, val):
14+
if not root:
15+
return 0
16+
left = self.getMaxLen(root.left, root.val)
17+
right = self.getMaxLen(root.right, root.val)
18+
self.maxLen = max(self.maxLen, left + right)
19+
if root.val == val:
20+
return max(left, right) + 1
21+
return 0
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 左边结点都比根结点小,右边结点都比根结点大
2+
class Solution(object):
3+
def searchBST(self, root, val):
4+
if not root:
5+
return
6+
if root.val == val:
7+
return root
8+
elif root.val > val:
9+
return self.searchBST(root.left, val)
10+
else:
11+
return self.searchBST(root.right, val)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def insertIntoBST(self, root, val):
3+
if not root:
4+
return TreeNode(val)
5+
if val < root.val:
6+
root.left = self.insertIntoBST(root.left, val)
7+
if val > root.val:
8+
root.right = self.insertIntoBST(root.right, val)
9+
return root
10+
11+
################## 递归思路 #################
12+
# 方法作用极简化:先判断无结点和1个结点的情况
13+
# 方法作用描述:参数给一个根和值,将该值的结点插入到根的左右孩子,然后返回插入后的根结点
14+
# 递归:由于左右孩子有同样需求,给定值结点部分使用递归得到插入后的根结点,将返回结果赋给根结点的左右孩子
15+
class Solution(object):
16+
def insertIntoBST(self, root, val):
17+
if not root:
18+
return TreeNode(val)
19+
if val < root.val:
20+
root.left = TreeNode(val)
21+
if val > root.val:
22+
root.right = TreeNode(val)
23+
return root

leetcode/707.设计链表.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 用数组模拟
2+
class MyLinkedList(object):
3+
4+
def __init__(self):
5+
self.arr = []
6+
7+
def get(self, index):
8+
if index > len(self.arr)-1 or index < 0:
9+
return -1
10+
return self.arr[index]
11+
12+
def addAtHead(self, val):
13+
self.arr.insert(0, val)
14+
15+
def addAtTail(self, val):
16+
self.arr.append(val)
17+
18+
def addAtIndex(self, index, val):
19+
if index == len(self.arr):
20+
self.arr.append(val)
21+
elif index > len(self.arr) or index < 0:
22+
return
23+
else:
24+
self.arr.insert(index, val)
25+
26+
def deleteAtIndex(self, index):
27+
if index > len(self.arr)-1 or index < 0:
28+
return
29+
self.arr.pop(index)

leetcode/725.分隔链表.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def splitListToParts(self, root, k):
3+
# 计算链表长度
4+
count = 0
5+
head = root
6+
while head:
7+
count += 1
8+
head = head.next
9+
10+
# nums代表每组的长度,rem代表前面几组可以再加1个结点
11+
nums = count/k
12+
rem = count%k
13+
res = []
14+
for _ in range(k):
15+
head = h = ListNode(0)
16+
for _ in range(nums):
17+
head.next = ListNode(root.val)
18+
head = head.next
19+
root = root.next
20+
if rem:
21+
head.next = ListNode(root.val)
22+
root = root.next
23+
rem -= 1
24+
res.append(h.next)
25+
return res

leetcode/814.二叉树剪枝.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def pruneTree(self, root):
3+
if not root:
4+
return
5+
root.left = self.pruneTree(root.left)
6+
root.right = self.pruneTree(root.right)
7+
if not root.left and not root.right and root.val == 0:
8+
return
9+
return root
10+
11+
################## 递归思路 #################
12+
# 方法作用极简化:先判断无结点和1个结点的情况
13+
# 方法作用描述:参数给一个结点,如果结点为0则返回空,即剪掉该结点,否则返回该结点
14+
# 递归:由于左右孩子有同样需求,使用递归,然后将返回结果赋给根结点的左右孩子
15+
class Solution(object):
16+
def pruneTree(self, root):
17+
if not root:
18+
return
19+
if root.val == 0:
20+
return
21+
return root

leetcode/817.链表组件.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 将G的元素放入Set,遍历head,若遇到一个元素在Set中并且下一个元素为空或者不在Set中,则将组件个数加1
2+
class Solution(object):
3+
def numComponents(self, head, G):
4+
count = 0
5+
g = set(G)
6+
while head:
7+
if head.val in g and (not head.next or head.next.val not in g):
8+
count += 1
9+
head = head.next
10+
return count

leetcode/834.树中距离之和.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def sumOfDistancesInTree(self, N, edges):
3+
d = collections.defaultdict(set)
4+
for s,t in edges:
5+
d[s].add(t)
6+
d[t].add(s)
7+
8+
count = [1]*N
9+
res = [0]*N
10+
11+
# 计算孩子到根的距离
12+
def dfs(root, vis):
13+
vis.add(root)
14+
for i in d[root]:
15+
if i not in vis:
16+
dfs(i, vis)
17+
# res[i]是用i作为根,所有孩子到i的距离之和
18+
count[root] += count[i]
19+
res[root] += res[i] + count[i]
20+
21+
# 计算根到孩子的距离
22+
def dfs2(root, vis):
23+
vis.add(root)
24+
for i in d[root]:
25+
if i not in vis:
26+
res[i] = res[root] - count[i] + N-count[i]
27+
dfs2(i, vis)
28+
29+
dfs(0, set())
30+
dfs2(0, set())
31+
return res
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def distanceK(self, root, target, K):
3+
# 字典的值类型是列表
4+
con = collections.defaultdict(list)
5+
6+
def connect(parent, child):
7+
if parent and child:
8+
con[parent.val].append(child.val)
9+
con[child.val].append(parent.val)
10+
if child.left:
11+
connect(child, child.left)
12+
if child.right:
13+
connect(child, child.right)
14+
15+
connect(None, root)
16+
res = [target.val]
17+
visited = set(res)
18+
for _ in range(K):
19+
res = [y for x in res for y in con[x] if y not in visited]
20+
# 合并
21+
visited |= set(res)
22+
return res
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def subtreeWithAllDeepest(self, root):
3+
# 判断左右子树深度是否相同,若相同则根结点为所求
4+
res = self.depth(root.left) - self.depth(root.right)
5+
if res == 0:
6+
return root
7+
elif res > 0:
8+
return self.subtreeWithAllDeepest(root.left)
9+
else:
10+
return self.subtreeWithAllDeepest(root.right)
11+
# 求树的深度
12+
def depth(self, root):
13+
if not root:
14+
return 0
15+
return 1 + max(self.depth(root.left), self.depth(root.right))

leetcode/872.叶子相似的树.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 获取叶值序列进行比较
2+
class Solution(object):
3+
def leafSimilar(self, root1, root2):
4+
def findLeaf(root):
5+
if not root:
6+
return []
7+
if not root.left and not root.right:
8+
return [root.val]
9+
return findLeaf(root.left) + findLeaf(root.right)
10+
11+
return findLeaf(root1) == findLeaf(root2)
12+

leetcode/876.链表的中间结点.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 方法一:用数组存放结点值,返回中间结点后面的值
2+
class Solution(object):
3+
def middleNode(self, head):
4+
res = []
5+
while head:
6+
res.append(head.val)
7+
head = head.next
8+
return res[len(res)/2:]
9+
10+
# 方法二:快的比慢的快一倍,当快的到底时,慢的刚好在中间
11+
class Solution(object):
12+
def middleNode(self, head):
13+
fast = slow = head
14+
while fast and fast.next:
15+
slow = slow.next
16+
fast = fast.next.next
17+
return slow
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 前序遍历的第一个元素,后续遍历的最后一个元素,是根节点
2+
# 从前序看 2 是左树的根节点,我们需要知道左树的长度,从后序找到2的位置,则4,5,2 是整个左树
3+
class Solution(object):
4+
def constructFromPrePost(self, pre, post):
5+
root = TreeNode(pre[0])
6+
pre = pre[1:]
7+
post = post[:-1]
8+
len_left = 0
9+
for i in post:
10+
if i == pre[0]:
11+
len_left += 1
12+
break
13+
else:
14+
len_left += 1
15+
if len_left > 0:
16+
root.left = self.constructFromPrePost(pre[:len_left], post[:len_left])
17+
if len(pre) - len_left > 0:
18+
root.right = self.constructFromPrePost(pre[len_left:], post[len_left:])
19+
return root

0 commit comments

Comments
 (0)