Skip to content

Commit 13e4f2d

Browse files
committed
[Leo] 5th Week solutions (added rest 3)
1 parent 469675e commit 13e4f2d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

3sum/Leo.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
4+
nums.sort()
5+
res = []
6+
7+
for i, ch in enumerate(nums):
8+
if i > 0 and nums[i] == nums[i - 1]: continue
9+
10+
l, r = i + 1, len(nums) - 1
11+
while l < r:
12+
threeSum = ch + nums[l] + nums[r]
13+
14+
if threeSum < 0:
15+
l += 1
16+
elif threeSum > 0:
17+
r -= 1
18+
else:
19+
res.append([ch, nums[l], nums[r]])
20+
l += 1
21+
while l < r and nums[l] == nums[l - 1]:
22+
l += 1
23+
24+
return res
25+
26+
## TC: O(n^2), SC: O(1)

longest-consecutive-sequence/Leo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: List[int]) -> int:
3+
4+
seen = set(nums)
5+
longest = 0
6+
7+
for n in seen:
8+
if (n - 1) not in seen:
9+
length = 1
10+
11+
while (n + length) in seen:
12+
length += 1
13+
14+
longest = max(length, longest)
15+
16+
return longest

product-of-array-except-self/Leo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
length = len(nums)
4+
res = [1] * length
5+
ltor = 1
6+
rtol = 1
7+
8+
for i in range(length):
9+
res[i] *= ltor
10+
ltor = ltor * nums[i]
11+
res[length - i - 1] *= rtol
12+
rtol = rtol * nums[length - i - 1]
13+
14+
return res
15+
16+
## TC: O(n), SC: O(1)

0 commit comments

Comments
 (0)