Skip to content

Commit 9ea05b5

Browse files
committed
Improved tasks 234-647
1 parent 8f15a36 commit 9ea05b5

File tree

5 files changed

+60
-93
lines changed
  • src/main/python

5 files changed

+60
-93
lines changed

src/main/python/g0201_0300/s0234_palindrome_linked_list/readme.md

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,16 @@ Given the `head` of a singly linked list, return `true` if it is a palindrome.
4040
# self.next = next
4141
class Solution:
4242
def isPalindrome(self, head: Optional[ListNode]) -> bool:
43-
# Calculate the length of the linked list
44-
length = 0
45-
right = head
46-
while right:
47-
right = right.next
48-
length += 1
49-
50-
# Reverse the right half of the list
51-
length //= 2
52-
right = head
53-
for _ in range(length):
54-
right = right.next
55-
56-
prev = None
57-
while right:
58-
next_node = right.next
59-
right.next = prev
60-
prev = right
61-
right = next_node
62-
63-
# Compare the left half and the right half
64-
for _ in range(length):
65-
if head and prev and head.val == prev.val:
66-
head = head.next
67-
prev = prev.next
68-
else:
43+
arr = []
44+
while head:
45+
arr.append(head.val)
46+
head = head.next
47+
l = 0
48+
r = len(arr) -1
49+
while l <= r:
50+
if arr[l] != arr[r]:
6951
return False
52+
l+=1
53+
r-=1
7054
return True
7155
```

src/main/python/g0201_0300/s0239_sliding_window_maximum/readme.md

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,17 @@ Return _the max sliding window_.
5959
## Solution
6060

6161
```python
62-
from collections import deque
63-
6462
class Solution:
6563
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
66-
n = len(nums)
67-
res = [0] * (n - k + 1)
68-
dq = deque()
69-
i, j = 0, 0
70-
x = 0
71-
72-
while j < len(nums):
73-
while dq and dq[-1] < nums[j]:
74-
dq.pop()
75-
dq.append(nums[j])
76-
77-
if j - i + 1 == k:
78-
res[x] = dq[0]
79-
x += 1
80-
if dq[0] == nums[i]:
81-
dq.popleft()
82-
i += 1
83-
j += 1
84-
85-
return res
64+
q = deque()
65+
result = []
66+
for right in range(len(nums)):
67+
while q and nums[right] > nums[q[-1]]:
68+
q.pop()
69+
q.append(right)
70+
if q[0] <= right - k:
71+
q.popleft()
72+
if right >= k - 1:
73+
result.append(nums[q[0]])
74+
return result
8675
```

src/main/python/g0401_0500/s0437_path_sum_iii/readme.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,21 @@ The path does not need to start or end at the root or a leaf, but it must go dow
4141
# self.left = left
4242
# self.right = right
4343
class Solution:
44-
def __init__(self):
45-
self.count = 0
46-
4744
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
48-
if root is None:
49-
return 0
50-
self.helper(root, targetSum, 0)
51-
self.pathSum(root.left, targetSum)
52-
self.pathSum(root.right, targetSum)
53-
return self.count
45+
def dfs(node: TreeNode, targetSum: int, curr_sum: int) -> None:
46+
if not node:
47+
return
48+
49+
curr_sum += node.val
50+
self.count += self.prefix_sum.get(curr_sum - targetSum, 0)
51+
self.prefix_sum[curr_sum] = self.prefix_sum.get(curr_sum, 0) + 1
52+
dfs(node.left, targetSum, curr_sum)
53+
dfs(node.right, targetSum, curr_sum)
5454

55-
def helper(self, node: TreeNode, targetSum: int, currSum: int) -> None:
56-
if node is None:
57-
return
58-
currSum += node.val
59-
if targetSum == currSum:
60-
self.count += 1
61-
if node.left is not None:
62-
self.helper(node.left, targetSum, currSum)
63-
if node.right is not None:
64-
self.helper(node.right, targetSum, currSum)
55+
self.prefix_sum[curr_sum] -= 1
56+
57+
self.count = 0
58+
self.prefix_sum = {0: 1}
59+
dfs(root, targetSum, 0)
60+
return self.count
6561
```

src/main/python/g0501_0600/s0560_subarray_sum_equals_k/readme.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,14 @@ from collections import defaultdict
3232

3333
class Solution:
3434
def subarraySum(self, nums: List[int], k: int) -> int:
35-
tempSum = 0
36-
ret = 0
37-
sumCount = defaultdict(int)
38-
sumCount[0] = 1
35+
sub_num = {0:1}
36+
total, count = 0, 0
37+
38+
for n in nums:
39+
total += n
40+
if total - k in sub_num:
41+
count += sub_num[total-k]
42+
sub_num[total] = 1 + sub_num.get(total, 0)
3943

40-
for i in nums:
41-
tempSum += i
42-
if tempSum - k in sumCount:
43-
ret += sumCount[tempSum - k]
44-
sumCount[tempSum] += 1
45-
46-
return ret
44+
return count
4745
```

src/main/python/g0601_0700/s0647_palindromic_substrings/readme.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ A **substring** is a contiguous sequence of characters within the string.
3636

3737
```python
3838
class Solution:
39-
def expand(self, a: List[str], l: int, r: int, res: List[int]) -> None:
40-
while l >= 0 and r < len(a):
41-
if a[l] != a[r]:
42-
return
43-
else:
44-
res[0] += 1
45-
l -= 1
46-
r += 1
47-
4839
def countSubstrings(self, s: str) -> int:
49-
a = list(s)
50-
res = [0]
51-
for i in range(len(a)):
52-
self.expand(a, i, i, res)
53-
self.expand(a, i, i + 1, res)
54-
return res[0]
40+
result = 0
41+
i = 0
42+
while i < len(s):
43+
l = i
44+
while i < len(s) and s[i] == s[l]:
45+
i+=1
46+
r = i - 1
47+
for j in range(r-l+1):
48+
result += j+1
49+
while l-1>=0 and r+1 < len(s) and s[l-1] == s[r+1]:
50+
result+=1
51+
52+
l-=1
53+
r+=1
54+
return result
5555
```

0 commit comments

Comments
 (0)