Skip to content

Commit 0e29bf1

Browse files
committed
added problems
1 parent 6afbf34 commit 0e29bf1

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def fourSum(self, nums, target):
3+
self.res = []
4+
if len(nums) < 4: return self.res
5+
6+
nums.sort()
7+
if nums[0] * 4 > target or nums[-1] * 4 < target: return self.res
8+
9+
for i in range(len(nums)-3):
10+
if i > 0 and nums[i] == nums[i-1]:
11+
continue
12+
for j in range(i+1, len(nums)-2):
13+
if j > i + 1 and nums[j] == nums[j-1]:
14+
continue
15+
self.findPair(nums, target, i, j)
16+
17+
return self.res
18+
19+
def findPair(self, nums, target, a_idx, b_idx):
20+
l = b_idx + 1
21+
r = len(nums) - 1
22+
23+
while l < r:
24+
a, b, c, d = nums[a_idx], nums[b_idx], nums[l], nums[r]
25+
total = a + b + c + d
26+
if total == target:
27+
self.res.append([a, b, c, d])
28+
l += 1
29+
r -= 1
30+
while l < r and nums[l] == nums[l-1]:
31+
l += 1
32+
while l < r and nums[r] == nums[r+1]:
33+
r -= 1
34+
elif total < target:
35+
l += 1
36+
else:
37+
r -= 1
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def backspaceCompare(self, S: str, T: str) -> bool:
3+
i1 = len(S) - 1
4+
i2 = len(T) - 1
5+
6+
while i1 >= 0 or i2 >= 0:
7+
i1 = self.nextIndex(S, i1)
8+
i2 = self.nextIndex(T, i2)
9+
if i1 < 0 and i2 < 0:
10+
return True
11+
if i1 < 0 or i2 < 0:
12+
return False
13+
if S[i1] != T[i2]:
14+
return False
15+
i1 -= 1
16+
i2 -= 1
17+
18+
return True
19+
20+
def nextIndex(self, string, idx):
21+
backspace_count = 0
22+
23+
while idx >= 0:
24+
if string[idx] == '#':
25+
backspace_count += 1
26+
elif backspace_count > 0:
27+
backspace_count -= 1
28+
else:
29+
break
30+
idx -= 1
31+
32+
return idx
33+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def findUnsortedSubarray(self, nums):
3+
n = len(nums)
4+
l = 0
5+
r = n - 1
6+
7+
while l < n - 1 and nums[l] <= nums[l+1]:
8+
l += 1
9+
10+
if l == n-1: return 0
11+
12+
while r > 0 and nums[r] >= nums[r-1]:
13+
r -= 1
14+
15+
sub_max = float('-inf')
16+
sub_min = float('inf')
17+
for i in range(l, r+1):
18+
sub_max = max(sub_max, nums[i])
19+
sub_min = min(sub_min, nums[i])
20+
21+
while l > 0 and nums[l-1] > sub_min:
22+
l -= 1
23+
24+
while r < n-1 and nums[r+1] < sub_max:
25+
r += 1
26+
27+
return r - l + 1

0 commit comments

Comments
 (0)