26. Remove Duplicates from Sorted Array
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums: return 0
count = 0
for i in range(1, len(nums)):
if nums[count] != nums[i]:
count+=1
nums[count] = nums[i]
return count + 1
27. Remove Element
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
if not nums: return 0
ans = 0
for i in range(0, len(nums)):
if nums[i] != val:
nums[ans] = nums[i]
ans+=1
return ans
80. Remove Duplicates from Sorted Array II
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums: return 0
count = 2
for i in range(2, len(nums)):
if nums[i] != nums[count - 2]:
nums[count] = nums[i]
count+=1
return count
11. Container With Most Water
class Solution:
def maxArea(self, height: List[int]) -> int:
ans = 0
left, right = 0, len(height) - 1
while left < right:
ans = max(min(height[left], height[right]) * (right - left), ans)
if height[left] < height[right]:
left += 1
else:
right -= 1
return ans
16. 3Sum Closest
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums = sorted(nums)
ans = nums[0] + nums[1] + nums[2]
for i in range(0, len(nums) - 2):
start, end = i + 1, len(nums) - 1
while start < end:
sum = nums[i] + nums[start] + nums[end]
if sum > target:
end -= 1
else:
start += 1
if abs(target - sum) < abs(target - ans):
ans = sum
return ans
42. Trapping Rain Water
class Solution:
def trap(self, height: List[int]) -> int:
left, right = 0, len(height) - 1
ans = 0
leftMax, rightMax = 0, 0
while left < right:
if height[left] < height[right]:
leftMax = max(height[left], leftMax)
ans += leftMax - height[left]
left += 1
else:
rightMax = max(height[right], rightMax)
ans += rightMax - height[right]
right -= 1
return ans
581. Shortest Unsorted Continuous Subarray
class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
start = -1
end = -1
minimum = nums[len(nums) - 1]
maximum = nums[0]
for i in range(1, len(nums)):
maximum = max(maximum, nums[i])
minimum = min(minimum, nums[len(nums) - 1 - i])
if nums[i] < maximum:
end = i
if nums[len(nums) - 1 - i] > minimum:
start = len(nums) - 1 - i
return 0 if end==start else end - start + 1
142. Linked List Cycle II
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head == None or head.next == None: return None
slow = head
fast = head
while fast != None and fast.next != None:
slow = slow.next
fast = fast.next.next
if fast == slow:
slow2 = head
while slow != slow2:
slow = slow.next
slow2 = slow2.next
return slow
return None
283. Move Zeroes
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
start = 0
for i in range(0, len(nums)):
if nums[i] != 0:
nums[start] = nums[i]
start += 1
while start < len(nums):
nums[start] = 0
start += 1
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
j = 0
for i in range(0, len(nums)):
if nums[i] != 0:
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
j += 1