If you find this useful, check out the full guide:
I Broke into NVIDIA with this Leetcode Study Guide
Given an integer array nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Solution: Keep track of a visited set and add items to it as you iterate through the list. If the current element is already in the visited set, return True.
def containsDuplicate(self, nums: List[int]) -> bool:
visited = set()
for num in nums:
if num in visited: return True
visited.add(num)
return False
Write a function that reverses a string. The input string is given as an array of characters s
. You must do this by modifying the input array in-place with O(1)
extra memory.
Solution: Reverse the input string in-place by swapping characters from the start and end of the array until the two pointers meet in the middle.
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
start=0
end=len(s)-1
while(start<end):
temp = s[start]
s[start]=s[end]
s[end]=temp
start+=1
end-=1