Welcome to my post dedicated to solving LeetCode problems!
Here, we explore various coding challenges and provide detailed solutions to help you enhance your problem-solving skills.
Problem List
Arrays & Hashing
Two Pointers
Solutions
Pascal's Triangle II 🔼
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
if rowIndex == 0:
return [1]
output = [1, 1]
for j in range(1, rowIndex):
l = len(output)
if l % 2 == 0:
mid = int(l/2)
output.insert(mid, output[mid]*2)
else:
mid = l//2
val = output[mid] + output[mid-1]
output[mid] = val
output.insert(mid+1, val)
l = l + 1
for i in range(mid-1, 0, -1):
output[i] = output[i] + output[i-1]
output[l - i - 1] = output[i]
return output
- Time Complexity - O(n2)
- Space Complexity - O(n)
Contains Duplicate 🔼
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
counts = {}
for num in nums:
counts[num] = counts.get(num, 0) + 1
if counts[num] > 1:
return True
return False
- Time Complexity - O(n)
- Space Complexity - O(n)
Reverse String 🔼
class Solution:
def reverseString(self, s: List[str]) -> None:
l, r = 0, len(s) - 1
while l < r:
s[r], s[l] = s[l], s[r]
l += 1
r -= 1
- Time Complexity - O(n)
- Space Complexity - O(1)