Skip to content

Commit 8d1b787

Browse files
committed
update
1 parent 4e02b7a commit 8d1b787

11 files changed

+314
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# @lc app=leetcode.cn id=124 lang=python3
3+
#
4+
# [124] 二叉树中的最大路径和
5+
#
6+
7+
8+
# @lc code=start
9+
# Definition for a binary tree node.
10+
# class TreeNode:
11+
# def __init__(self, val=0, left=None, right=None):
12+
# self.val = val
13+
# self.left = left
14+
# self.right = right
15+
class Solution:
16+
17+
def maxPathSum(self, root: Optional[TreeNode]) -> int:
18+
result = -2000
19+
20+
def _maxPathSum(root):
21+
nonlocal result
22+
23+
if root is None:
24+
return 0
25+
26+
left = max([_maxPathSum(root.left), 0])
27+
right = max([_maxPathSum(root.right), 0])
28+
val = root.val
29+
30+
result = max([val + left + right, result])
31+
32+
return max([val, val + left, val + right])
33+
34+
_maxPathSum(root)
35+
return result
36+
37+
# @lc code=end

code/264.丑数-ii.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# @lc app=leetcode.cn id=264 lang=python3
3+
#
4+
# [264] 丑数 II
5+
#
6+
import heapq
7+
# @lc code=start
8+
class Solution:
9+
def nthUglyNumber(self, n: int) -> int:
10+
11+
nums = [1]
12+
eliminate = set()
13+
for _ in range(n - 1):
14+
curr = heapq.heappop(nums)
15+
16+
for i in [2, 3, 5]:
17+
n = curr * i
18+
if n not in eliminate:
19+
heapq.heappush(nums, n)
20+
eliminate.add(n)
21+
22+
return heapq.heappop(nums)
23+
# @lc code=end
24+

code/313.超级丑数.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# @lc app=leetcode.cn id=313 lang=python3
3+
#
4+
# [313] 超级丑数
5+
#
6+
7+
import heapq
8+
# @lc code=start
9+
class Solution:
10+
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
11+
nums = [1]
12+
en = set()
13+
for _ in range(n - 1):
14+
n = heapq.heappop(nums)
15+
16+
for p in primes:
17+
a = n * p
18+
if a not in en:
19+
en.add(a)
20+
heapq.heappush(nums, a)
21+
22+
return heapq.heappop(nums)
23+
# @lc code=end
24+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# @lc app=leetcode.cn id=318 lang=python3
3+
#
4+
# [318] 最大单词长度乘积
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def maxProduct(self, words: List[str]) -> int:
10+
max_ = 0
11+
words = sorted(words, key=lambda k: len(k), reverse=True)
12+
wlen = len(words)
13+
for i in range(wlen):
14+
w1 = words[i]
15+
w1l = len(w1)
16+
w1s = set(w1)
17+
for j in range(i + 1, wlen):
18+
w2 = words[j]
19+
w2s = set(w2)
20+
if len(w1s | w2s) == (len(w1s) + len(w2s)):
21+
max_ = max([w1l * len(w2), max_])
22+
break
23+
return max_
24+
# @lc code=end

code/319.灯泡开关.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# @lc app=leetcode.cn id=319 lang=python3
3+
#
4+
# [319] 灯泡开关
5+
#
6+
import math
7+
# @lc code=start
8+
class Solution:
9+
def bulbSwitch(self, n: int) -> int:
10+
return int(math.sqrt(n))
11+
# @lc code=end

code/324.摆动排序-ii.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#
2+
# @lc app=leetcode.cn id=324 lang=python3
3+
#
4+
# [324] 摆动排序 II
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def wiggleSort(self, nums: List[int]) -> None:
10+
"""
11+
Do not return anything, modify nums in-place instead.
12+
"""
13+
arrs = sorted(nums)
14+
j = 1
15+
al = len(arrs)
16+
al2 = (len(arrs) + 1) // 2
17+
for i in range(0, al, 2):
18+
nums[i] = arrs[al2 - j]
19+
if i + 1 < al:
20+
nums[i + 1] = arrs[al - j]
21+
j += 1
22+
# @lc code=end
23+

code/328.奇偶链表.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# @lc app=leetcode.cn id=328 lang=python3
3+
#
4+
# [328] 奇偶链表
5+
#
6+
7+
# @lc code=start
8+
# Definition for singly-linked list.
9+
# class ListNode:
10+
# def __init__(self, val=0, next=None):
11+
# self.val = val
12+
# self.next = next
13+
class Solution:
14+
def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
15+
if head is None:
16+
return head
17+
18+
root = head
19+
r2s = None
20+
r2 = None
21+
22+
while root is not None and root.next is not None:
23+
if r2 is None:
24+
r2 = root.next
25+
r2s = root.next
26+
else:
27+
r2.next = root.next
28+
r2 = r2.next
29+
root.next = root.next.next
30+
if root.next is None:
31+
break
32+
root = root.next
33+
if r2 is not None:
34+
r2.next = None
35+
root.next = r2s
36+
return head
37+
# @lc code=end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# @lc app=leetcode.cn id=700 lang=python3
3+
#
4+
# [700] 二叉搜索树中的搜索
5+
#
6+
7+
# @lc code=start
8+
# Definition for a binary tree node.
9+
# class TreeNode:
10+
# def __init__(self, val=0, left=None, right=None):
11+
# self.val = val
12+
# self.left = left
13+
# self.right = right
14+
class Solution:
15+
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
16+
if root is None:
17+
return None
18+
if val == root.val:
19+
return root
20+
elif val > root.val:
21+
return self.searchBST(root.right, val)
22+
return self.searchBST(root.left, val)
23+
# @lc code=end
24+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# @lc app=leetcode.cn id=703 lang=python3
3+
#
4+
# [703] 数据流中的第 K 大元素
5+
#
6+
7+
# @lc code=start
8+
class KthLargest:
9+
10+
def __init__(self, k: int, nums: List[int]):
11+
self.k = k
12+
self.nums = sorted(nums, reverse=True)[:k]
13+
14+
def find_val(self, val):
15+
s = 0
16+
e = len(self.nums)
17+
while s < e:
18+
mid = (s + e) // 2
19+
n = self.nums[mid]
20+
if n == val:
21+
return mid
22+
elif n < val:
23+
e = mid - 1
24+
else:
25+
s = mid + 1
26+
return s
27+
28+
def add(self, val: int) -> int:
29+
30+
if len(self.nums) >= self.k:
31+
n = self.nums[self.k - 1]
32+
if n > val:
33+
return n
34+
35+
i = self.find_val(val)
36+
37+
if len(self.nums) > i:
38+
v = self.nums[i]
39+
if v > val:
40+
i += 1
41+
self.nums = self.nums[:i] + [val] + self.nums[i:]
42+
self.nums = self.nums[:self.k]
43+
else:
44+
self.nums.append(val)
45+
46+
return self.nums[self.k - 1]
47+
48+
# Your KthLargest object will be instantiated and called as such:
49+
# obj = KthLargest(k, nums)
50+
# param_1 = obj.add(val)
51+
# @lc code=end
52+

code/704.二分查找.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# @lc app=leetcode.cn id=704 lang=python3
3+
#
4+
# [704] 二分查找
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def search(self, nums: List[int], target: int) -> int:
10+
s = 0
11+
e = len(nums)
12+
while s < e:
13+
mid = (s + e) // 2
14+
if nums[mid] == target:
15+
return mid
16+
elif nums[mid] > target:
17+
e = mid - 1
18+
else:
19+
s = mid + 1
20+
if len(nums) > s and nums[s] == target:
21+
return s
22+
return -1
23+
# @lc code=end
24+

0 commit comments

Comments
 (0)