Skip to content

Commit 4e02b7a

Browse files
committed
update
1 parent ee9246b commit 4e02b7a

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

code/306.累加数.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#
2+
# @lc app=leetcode.cn id=306 lang=python3
3+
#
4+
# [306] 累加数
5+
#
6+
7+
8+
# @lc code=start
9+
class Solution:
10+
11+
def _add(self, s1, s2):
12+
s1l = len(s1)
13+
s2l = len(s2)
14+
s = 0
15+
res = ''
16+
for i in range(1, max(s1l, s2l) + 1):
17+
18+
a = s1[s1l - i] if i <= s1l else 0
19+
b = s2[s2l - i] if i <= s2l else 0
20+
x = int(a) + int(b) + s
21+
s = 0
22+
if x >= 10:
23+
s = 1
24+
x %= 10
25+
res = f'{x}{res}'
26+
27+
if s == 1:
28+
res = f'1{res}'
29+
30+
return res
31+
32+
33+
def _isAdditiveNumber(self, i, j, num):
34+
s = self._add(num[: i], num[i: i + j])
35+
36+
n = num[i + j:]
37+
if n.startswith(s):
38+
if len(n) == len(s):
39+
return True
40+
else:
41+
return self._isAdditiveNumber(j, len(s), num[i:])
42+
return False
43+
44+
def isAdditiveNumber(self, num: str) -> bool:
45+
if len(num) < 3:
46+
return False
47+
48+
n2 = len(num) // 2 + 1
49+
for i in range(1, n2):
50+
if num[0] == '0' and i > 1:
51+
break
52+
for j in range(1, n2):
53+
if num[i] == '0' and j > 1:
54+
break
55+
if i + j == len(num):
56+
return False
57+
if self._isAdditiveNumber(i, j, num):
58+
return True
59+
60+
return False
61+
# @lc code=end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# @lc app=leetcode.cn id=307 lang=python3
3+
#
4+
# [307] 区域和检索 - 数组可修改
5+
#
6+
7+
# @lc code=start
8+
class NumArray:
9+
10+
def __init__(self, nums: List[int]):
11+
self.__data = []
12+
self.__nums = nums
13+
self.__update = {}
14+
sum_ = 0
15+
for num in nums:
16+
sum_ += num
17+
self.__data.append(sum_)
18+
19+
def update(self, index: int, val: int) -> None:
20+
self.__update[index] = val
21+
if len(self.__update) == 500:
22+
for key in self.__update:
23+
self.__nums[key] = self.__update[key]
24+
25+
self.__data = []
26+
sum_ = 0
27+
for num in self.__nums:
28+
sum_ += num
29+
self.__data.append(sum_)
30+
self.__update = {}
31+
32+
def sumRange(self, left: int, right: int) -> int:
33+
l = self.__data[left - 1] if left - 1 >= 0 else 0
34+
sum_ = self.__data[right] - l
35+
for i in self.__update:
36+
if left <= i <= right:
37+
sum_ += self.__update[i] - self.__nums[i]
38+
return sum_
39+
40+
# Your NumArray object will be instantiated and called as such:
41+
# obj = NumArray(nums)
42+
# obj.update(index,val)
43+
# param_2 = obj.sumRange(left,right)
44+
# @lc code=end
45+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# @lc app=leetcode.cn id=309 lang=python3
3+
#
4+
# [309] 买卖股票的最佳时机含冷冻期
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def maxProfit(self, prices: List[int]) -> int:
10+
if len(prices) == 1:
11+
return 0
12+
13+
buy = -prices[0]
14+
stop = 0
15+
sell = 0
16+
17+
for price in prices[1:]:
18+
buy, stop, sell = max([stop - price, buy]), max([stop, sell]), max([buy + price, sell])
19+
20+
return max([stop, sell])
21+
# @lc code=end

code/310.最小高度树.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#
2+
# @lc app=leetcode.cn id=310 lang=python3
3+
#
4+
# [310] 最小高度树
5+
#
6+
7+
from collections import deque
8+
# @lc code=start
9+
class Solution:
10+
11+
def heightTree(self, i, n, tree):
12+
13+
deques = deque([i])
14+
visits = {i}
15+
paths = [-1] * n
16+
while len(deques):
17+
node = deques.popleft()
18+
ns = tree.get(node)
19+
for n in ns:
20+
if n not in visits:
21+
visits.add(n)
22+
paths[n] = node
23+
deques.append(n)
24+
return node, paths
25+
26+
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
27+
28+
if n == 1:
29+
return [0]
30+
31+
tree = {}
32+
for edge in edges:
33+
tree.setdefault(edge[0], []).append(edge[1])
34+
tree.setdefault(edge[1], []).append(edge[0])
35+
36+
x, _ = self.heightTree(0, n, tree)
37+
y, paths = self.heightTree(x, n, tree)
38+
39+
ps = []
40+
while paths[y] != -1:
41+
ps.append(y)
42+
y = paths[y]
43+
ps.append(x)
44+
45+
psl2 = len(ps) // 2
46+
res = [ps[psl2]]
47+
if len(ps) % 2 == 0:
48+
res.append(ps[psl2 - 1])
49+
return res
50+
51+
# @lc code=end

0 commit comments

Comments
 (0)