Skip to content

Commit 2ee7477

Browse files
committed
update code files
1 parent 3b00f02 commit 2ee7477

File tree

149 files changed

+3758
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+3758
-0
lines changed

code/1004.max-consecutive-ones-iii.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# @lc app=leetcode id=1004 lang=python3
3+
#
4+
# [1004] Max Consecutive Ones III
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def longestOnes(self, A: List[int], K: int) -> int:
10+
res = 0
11+
l = 0
12+
for r in range(len(A)):
13+
if K >= 0:
14+
res = max(res, r - l)
15+
if A[r]: continue
16+
K -= 1
17+
while K < 0:
18+
while A[l]: l += 1
19+
K += 1
20+
l += 1
21+
if K >= 0: res = max(res, r - l + 1)
22+
return res
23+
# @lc code=end
24+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# @lc app=leetcode id=1013 lang=python3
3+
#
4+
# [1013] Partition Array Into Three Parts With Equal Sum
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def canThreePartsEqualSum(self, A: List[int]) -> bool:
10+
11+
# @lc code=end
12+

code/1049.last-stone-weight-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 id=1049 lang=python3
3+
#
4+
# [1049] Last Stone Weight II
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
'''
10+
dp[stone + i] = max(dp[stone + i], dp[i] + stone)
11+
'''
12+
def lastStoneWeightII(self, stones: List[int]) -> int:
13+
s = sum(stones)
14+
m = s // 2
15+
16+
dp = [0 for _ in range(m + 1)]
17+
for stone in stones:
18+
for i in range(m + 1, -1, -1):
19+
if stone + i <= m:
20+
dp[stone + i] = max(dp[stone + i], dp[i] + stone)
21+
return s - 2*(dp[-1])
22+
# @lc code=end
23+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# @lc app=leetcode id=105 lang=python3
3+
#
4+
# [105] Construct Binary Tree from Preorder and Inorder Traversal
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 buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
16+
def build_tree(pre, i):
17+
if not pre:
18+
return None
19+
node = TreeNode(pre[0])
20+
index = i.index(pre[0])
21+
node.left = build_tree(pre[1:1+index], i[:index])
22+
node.right = build_tree(pre[1+index:], i[index+1:])
23+
return node
24+
return build_tree(preorder, inorder)
25+
# @lc code=end
26+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# @lc app=leetcode id=106 lang=python3
3+
#
4+
# [106] Construct Binary Tree from Inorder and Postorder Traversal
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 buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
16+
def build_tree(i, post):
17+
if not post:
18+
return None
19+
node = TreeNode(post[-1])
20+
index = i.index(post[-1])
21+
node.left = build_tree(i[:index], post[:index])
22+
node.right = build_tree(i[index + 1:], post[index:-1])
23+
return node
24+
return build_tree(inorder, postorder)
25+
# @lc code=end
26+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# @lc app=leetcode id=1143 lang=python3
3+
#
4+
# [1143] Longest Common Subsequence
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
10+
m, n = len(text1), len(text2)
11+
dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
12+
for i in range(1, m + 1):
13+
for j in range(1, n + 1):
14+
if text1[i - 1] == text2[j - 1]:
15+
dp[i][j] = dp[i - 1][j - 1] + 1
16+
else:
17+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
18+
return dp[m][n]
19+
# @lc code=end
20+

code/120.triangle.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# @lc app=leetcode id=120 lang=python3
3+
#
4+
# [120] Triangle
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def minimumTotal(self, triangle: List[List[int]]) -> int:
10+
return self.dp(triangle, 0, len(triangle) - 1)[0]
11+
12+
def dp(self, triangle, depth, m):
13+
if depth == m:
14+
return triangle[depth]
15+
last = self.dp(triangle, depth + 1, m)
16+
res = []
17+
for i, n in enumerate(triangle[depth]):
18+
res.append(n + min(last[i], last[i + 1]))
19+
return res
20+
21+
# @lc code=end
22+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# @lc app=leetcode id=1234 lang=python3
3+
#
4+
# [1234] Replace the Substring for Balanced String
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def balancedString(self, s: str) -> int:
10+
cnt = collections.Counter(s)
11+
res = n = len(s)
12+
l = 0
13+
for r, c in enumerate(s):
14+
cnt[c] -= 1
15+
while l < n and all(n//4 >= cnt[c] for c in 'QWER'):
16+
res = min(r - l + 1, res)
17+
cnt[s[l]] += 1
18+
l += 1
19+
return res
20+
# @lc code=end
21+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# @lc app=leetcode id=1248 lang=python3
3+
#
4+
# [1248] Count Number of Nice Subarrays
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
10+
def Most(k):
11+
res = 0
12+
l = 0
13+
for r in range(len(nums)):
14+
k -= nums[r] % 2
15+
while k < 0:
16+
k += nums[l] % 2
17+
l += 1
18+
res += r - l + 1
19+
return res
20+
return Most(k) - Most(k - 1)
21+
# @lc code=end
22+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# @lc app=leetcode id=128 lang=python3
3+
#
4+
# [128] Longest Consecutive Sequence
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def longestConsecutive(self, nums: List[int]) -> int:
10+
11+
# @lc code=end
12+

code/141.linked-list-cycle.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# @lc app=leetcode id=141 lang=python3
3+
#
4+
# [141] Linked List Cycle
5+
#
6+
7+
# @lc code=start
8+
# Definition for singly-linked list.
9+
# class ListNode:
10+
# def __init__(self, x):
11+
# self.val = x
12+
# self.next = None
13+
14+
class Solution:
15+
def hasCycle(self, head: ListNode) -> bool:
16+
if not head:
17+
return False
18+
fast, slow = head, head
19+
while fast.next and fast.next.next:
20+
fast = fast.next.next
21+
slow = slow.next
22+
if fast == slow:
23+
return True
24+
if fast != slow:
25+
return False
26+
27+
28+
# @lc code=end
29+

code/142.linked-list-cycle-ii.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# @lc app=leetcode id=142 lang=python3
3+
#
4+
# [142] Linked List Cycle II
5+
#
6+
7+
# @lc code=start
8+
# Definition for singly-linked list.
9+
# class ListNode:
10+
# def __init__(self, x):
11+
# self.val = x
12+
# self.next = None
13+
14+
class Solution:
15+
def detectCycle(self, head: ListNode) -> ListNode:
16+
slow, fast = head, head
17+
while fast and fast.next:
18+
slow = slow.next
19+
fast = fast.next.next
20+
if slow == fast:
21+
slow2 = head
22+
while slow != slow2:
23+
slow = slow.next
24+
slow2 = slow2.next
25+
return slow
26+
return None
27+
# @lc code=end
28+

code/15.3-sum.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#
2+
# @lc app=leetcode id=15 lang=python3
3+
#
4+
# [15] 3Sum
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def threeSum(self, nums: List[int]) -> List[List[int]]:
10+
nums.sort()
11+
res = []
12+
i = 0
13+
for i in range(len(nums)):
14+
if i != 0 and nums[i] == nums[i - 1]:
15+
continue
16+
l, r = i + 1, len(nums) - 1
17+
while l < r:
18+
if nums[l] + nums[r] == - nums[i]:
19+
res.append([nums[i], nums[l], nums[r]])
20+
l += 1
21+
r -= 1
22+
while l < r and nums[l - 1] == nums[l]:
23+
l += 1
24+
while l < r and nums[r + 1] == nums[r]:
25+
r -= 1
26+
elif nums[l] + nums[r] > - nums[i]:
27+
r -= 1
28+
else:
29+
l += 1
30+
return res
31+
# @lc code=end
32+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# @lc app=leetcode id=153 lang=python3
3+
#
4+
# [153] Find Minimum in Rotated Sorted Array
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def findMin(self, nums: List[int]) -> int:
10+
if not nums:
11+
return -1
12+
l, r = 0, len(nums) - 1
13+
res = float("inf")
14+
while l < r:
15+
m = (r - l) // 2 + l
16+
if nums[m] > nums[-1]:
17+
l = m + 1
18+
else:
19+
r = m
20+
return nums[l]
21+
# @lc code=end
22+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# @lc app=leetcode id=160 lang=python3
3+
#
4+
# [160] Intersection of Two Linked Lists
5+
#
6+
7+
# @lc code=start
8+
# Definition for singly-linked list.
9+
# class ListNode:
10+
# def __init__(self, x):
11+
# self.val = x
12+
# self.next = None
13+
14+
class Solution:
15+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
16+
if not headB or not headA:
17+
return None
18+
A, B = headA, headB
19+
while A != B:
20+
A = A.next if A else headB
21+
B = B.next if B else headA
22+
return A
23+
# @lc code=end
24+

code/206.reverse-linked-list.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#
2+
# @lc app=leetcode id=206 lang=python3
3+
#
4+
# [206] Reverse Linked List
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 reverseList(self, head: ListNode) -> ListNode:
15+
pre = None
16+
while head:
17+
cur = head
18+
head = head.next
19+
cur.next = pre
20+
pre = cur
21+
return pre
22+
# @lc code=end
23+

0 commit comments

Comments
 (0)