Skip to content

Commit f03b553

Browse files
author
huanghaifeng
committed
第三周作业#26
1 parent a00c6ea commit f03b553

11 files changed

+853
-0
lines changed

Week_03/id_26/LeetCode_102_26.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#
2+
# @lc app=leetcode.cn id=102 lang=python
3+
#
4+
# [102] 二叉树的层次遍历
5+
#
6+
# https://leetcode-cn.com/problems/binary-tree-level-order-traversal/description/
7+
#
8+
# algorithms
9+
# Medium (56.47%)
10+
# Likes: 213
11+
# Dislikes: 0
12+
# Total Accepted: 28.4K
13+
# Total Submissions: 50.2K
14+
# Testcase Example: '[3,9,20,null,null,15,7]'
15+
#
16+
# 给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
17+
#
18+
# 例如:
19+
# 给定二叉树: [3,9,20,null,null,15,7],
20+
#
21+
# ⁠ 3
22+
# ⁠ / \
23+
# ⁠ 9 20
24+
# ⁠ / \
25+
# ⁠ 15 7
26+
#
27+
#
28+
# 返回其层次遍历结果:
29+
#
30+
# [
31+
# ⁠ [3],
32+
# ⁠ [9,20],
33+
# ⁠ [15,7]
34+
# ]
35+
#
36+
#
37+
#
38+
# Definition for a binary tree node.
39+
# class TreeNode(object):
40+
# def __init__(self, x):
41+
# self.val = x
42+
# self.left = None
43+
# self.right = None
44+
45+
46+
class Solution(object):
47+
def levelOrder(self, root):
48+
"""
49+
:type root: TreeNode
50+
:rtype: List[List[int]]
51+
"""
52+
if not root:
53+
return []
54+
layer, ans = [root], []
55+
while layer:
56+
next_layer, vals = [], []
57+
for node in layer:
58+
vals.append(node.val)
59+
if node.left:
60+
next_layer.append(node.left)
61+
if node.right:
62+
next_layer.append(node.right)
63+
layer, ans = next_layer, ans + [vals]
64+
return ans

Week_03/id_26/LeetCode_104_26.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#
2+
# @lc app=leetcode.cn id=104 lang=python
3+
#
4+
# [104] 二叉树的最大深度
5+
#
6+
# https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/
7+
#
8+
# algorithms
9+
# Easy (69.23%)
10+
# Likes: 297
11+
# Dislikes: 0
12+
# Total Accepted: 51.2K
13+
# Total Submissions: 73.9K
14+
# Testcase Example: '[3,9,20,null,null,15,7]'
15+
#
16+
# 给定一个二叉树,找出其最大深度。
17+
#
18+
# 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
19+
#
20+
# 说明: 叶子节点是指没有子节点的节点。
21+
#
22+
# 示例:
23+
# 给定二叉树 [3,9,20,null,null,15,7],
24+
#
25+
# ⁠ 3
26+
# ⁠ / \
27+
# ⁠ 9 20
28+
# ⁠ / \
29+
# ⁠ 15 7
30+
#
31+
# 返回它的最大深度 3 。
32+
#
33+
#
34+
# Definition for a binary tree node.
35+
# class TreeNode(object):
36+
# def __init__(self, x):
37+
# self.val = x
38+
# self.left = None
39+
# self.right = None
40+
41+
42+
class Solution(object):
43+
def maxDepth(self, root):
44+
"""
45+
:type root: TreeNode
46+
:rtype: int
47+
"""
48+
if not root:
49+
return 0
50+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

Week_03/id_26/LeetCode_107_26.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#
2+
# @lc app=leetcode.cn id=107 lang=python
3+
#
4+
# [107] 二叉树的层次遍历 II
5+
#
6+
# https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/description/
7+
#
8+
# algorithms
9+
# Easy (60.77%)
10+
# Likes: 108
11+
# Dislikes: 0
12+
# Total Accepted: 17.6K
13+
# Total Submissions: 28.8K
14+
# Testcase Example: '[3,9,20,null,null,15,7]'
15+
#
16+
# 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
17+
#
18+
# 例如:
19+
# 给定二叉树 [3,9,20,null,null,15,7],
20+
#
21+
# ⁠ 3
22+
# ⁠ / \
23+
# ⁠ 9 20
24+
# ⁠ / \
25+
# ⁠ 15 7
26+
#
27+
#
28+
# 返回其自底向上的层次遍历为:
29+
#
30+
# [
31+
# ⁠ [15,7],
32+
# ⁠ [9,20],
33+
# ⁠ [3]
34+
# ]
35+
#
36+
#
37+
#
38+
# Definition for a binary tree node.
39+
# class TreeNode(object):
40+
# def __init__(self, x):
41+
# self.val = x
42+
# self.left = None
43+
# self.right = None
44+
45+
import collections
46+
47+
48+
class Solution(object):
49+
def levelOrderBottom(self, root):
50+
"""
51+
:type root: TreeNode
52+
:rtype: List[List[int]]
53+
"""
54+
queue, ans = collections.deque([(root, 0)]), []
55+
while queue:
56+
node, level = queue.popleft()
57+
if node:
58+
if len(ans) < level+1:
59+
ans.insert(0, [])
60+
ans[-level-1] += [node.val]
61+
queue.append((node.left, level+1))
62+
queue.append((node.right, level+1))
63+
return ans

Week_03/id_26/LeetCode_111_26.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#
2+
# @lc app=leetcode.cn id=111 lang=python
3+
#
4+
# [111] 二叉树的最小深度
5+
#
6+
# https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/
7+
#
8+
# algorithms
9+
# Easy (38.70%)
10+
# Likes: 125
11+
# Dislikes: 0
12+
# Total Accepted: 19K
13+
# Total Submissions: 49K
14+
# Testcase Example: '[3,9,20,null,null,15,7]'
15+
#
16+
# 给定一个二叉树,找出其最小深度。
17+
#
18+
# 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
19+
#
20+
# 说明: 叶子节点是指没有子节点的节点。
21+
#
22+
# 示例:
23+
#
24+
# 给定二叉树 [3,9,20,null,null,15,7],
25+
#
26+
# ⁠ 3
27+
# ⁠ / \
28+
# ⁠ 9 20
29+
# ⁠ / \
30+
# ⁠ 15 7
31+
#
32+
# 返回它的最小深度  2.
33+
#
34+
#
35+
# Definition for a binary tree node.
36+
# class TreeNode(object):
37+
# def __init__(self, x):
38+
# self.val = x
39+
# self.left = None
40+
# self.right = None
41+
42+
43+
class Solution(object):
44+
def minDepth(self, root):
45+
"""
46+
:type root: TreeNode
47+
:rtype: int
48+
"""
49+
if not root:
50+
return 0
51+
left = self.minDepth(root.left)
52+
right = self.minDepth(root.right)
53+
if not left or not right:
54+
return 1 + left + right
55+
return 1 + min(left, right)

Week_03/id_26/LeetCode_200_26.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#
2+
# @lc app=leetcode.cn id=200 lang=python
3+
#
4+
# [200] 岛屿数量
5+
#
6+
# https://leetcode-cn.com/problems/number-of-islands/description/
7+
#
8+
# algorithms
9+
# Medium (43.81%)
10+
# Likes: 167
11+
# Dislikes: 0
12+
# Total Accepted: 16K
13+
# Total Submissions: 36.5K
14+
# Testcase Example: '[["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]'
15+
#
16+
# 给定一个由 '1'(陆地)和
17+
# '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
18+
#
19+
# 示例 1:
20+
#
21+
# 输入:
22+
# 11110
23+
# 11010
24+
# 11000
25+
# 00000
26+
#
27+
# 输出: 1
28+
#
29+
#
30+
# 示例 2:
31+
#
32+
# 输入:
33+
# 11000
34+
# 11000
35+
# 00100
36+
# 00011
37+
#
38+
# 输出: 3
39+
#
40+
#
41+
#
42+
43+
44+
class Solution(object):
45+
def numIslands(self, grid):
46+
"""
47+
:type grid: List[List[str]]
48+
:rtype: int
49+
"""
50+
51+
dx, dy = [1, -1, 0, 0], [0, 0, 1, -1]
52+
53+
def sink(i, j):
54+
if i < 0 or i == len(grid) or j < 0 or j == len(
55+
grid[i]) or grid[i][j] == '0':
56+
return 0
57+
grid[i][j] = '0'
58+
for k in range(4):
59+
sink(i + dx[k], j + dy[k])
60+
return 1
61+
62+
ans = 0
63+
for i in range(len(grid)):
64+
for j in range(len(grid[i])):
65+
ans += sink(i, j)
66+
return ans
67+
68+
69+
# print(Solution().numIslands([["1", "1", "1", "1", "0"],
70+
# ["1", "1", "0", "1", "0"],
71+
# ["1", "1", "0", "0", "0"],
72+
# ["0", "0", "0", "0", "0"]]))
73+
74+
# print(Solution().numIslands([]))

Week_03/id_26/LeetCode_295_26.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#
2+
# @lc app=leetcode.cn id=295 lang=python
3+
#
4+
# [295] 数据流的中位数
5+
#
6+
# https://leetcode-cn.com/problems/find-median-from-data-stream/description/
7+
#
8+
# algorithms
9+
# Hard (35.43%)
10+
# Likes: 46
11+
# Dislikes: 0
12+
# Total Accepted: 3.3K
13+
# Total Submissions: 9.2K
14+
# Testcase Example: '["MedianFinder","addNum","addNum","findMedian","addNum","findMedian"]\n[[],[1],[2],[],[3],[]]'
15+
#
16+
# 中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
17+
#
18+
# 例如,
19+
#
20+
# [2,3,4] 的中位数是 3
21+
#
22+
# [2,3] 的中位数是 (2 + 3) / 2 = 2.5
23+
#
24+
# 设计一个支持以下两种操作的数据结构:
25+
#
26+
#
27+
# void addNum(int num) - 从数据流中添加一个整数到数据结构中。
28+
# double findMedian() - 返回目前所有元素的中位数。
29+
#
30+
#
31+
# 示例:
32+
#
33+
# addNum(1)
34+
# addNum(2)
35+
# findMedian() -> 1.5
36+
# addNum(3)
37+
# findMedian() -> 2
38+
#
39+
# 进阶:
40+
#
41+
#
42+
# 如果数据流中所有整数都在 0 到 100 范围内,你将如何优化你的算法?
43+
# 如果数据流中 99% 的整数都在 0 到 100 范围内,你将如何优化你的算法?
44+
#
45+
#
46+
#
47+
"""
48+
维护大顶堆、小顶堆,满足以下条件:
49+
50+
大顶堆中数字的个数 - 小顶堆数字的个数 = 0 or 1
51+
大顶堆的最大值 <= 小顶堆的最小值
52+
当长度为偶数时,中位数为大小顶堆的平均数 当长度为奇数时,中位数为大顶堆的最大值
53+
54+
注:python的heapq中只有小顶堆,所以存入时取负数则为大顶堆
55+
"""
56+
import heapq
57+
58+
59+
class MedianFinder(object):
60+
def __init__(self):
61+
"""
62+
initialize your data structure here.
63+
"""
64+
self.len = 0
65+
self.max_heap, self.min_heap = [], []
66+
67+
def addNum(self, num):
68+
"""
69+
:type num: int
70+
:rtype: None
71+
"""
72+
self.len += 1
73+
heapq.heappush(self.min_heap, -heapq.heappushpop(self.max_heap, num))
74+
if len(self.min_heap) > len(self.max_heap):
75+
heapq.heappush(self.max_heap, -heapq.heappop(self.min_heap))
76+
77+
def findMedian(self):
78+
"""
79+
:rtype: float
80+
"""
81+
if self.len & 1 == 0:
82+
return (self.max_heap[0] - self.min_heap[0]) / 2.0
83+
return self.max_heap[0]

0 commit comments

Comments
 (0)