classSolution:defmerge(self,nums1:List[int],m:int,nums2:List[int],n:int)->None:"""
Do not return anything, modify nums1 in-place instead.
"""# To attempt this problem, first think of traversing from the end
# then it is easy to realise we only need to pick the larger of the two choices (i,j)
# and put it at current position(k). if we pick i, decrement i, otherwise we picked j so
# decrement j. That's it!
i=m-1j=n-1k=m+n-1whilej>=0:ifi>=0andnums1[i]>nums2[j]:nums1[k]=nums1[i]i-=1else:nums1[k]=nums2[j]j-=1k-=1
classSolution:defremoveElement(self,nums:List[int],val:int)->int:# in place removal requires shifting. Or just skipping!
# keep a hare (i) and a tortoise (k) and instead of checking
# if nums[i] == val, check the opposite, or nums[i] != val, then
# set nums[tortoise] = nums[hare]. It will skip when nums[hare] == val
k=0foriinrange(len(nums)):ifnums[i]!=val:nums[k]=nums[i]k+=1returnk
classSolution:defremoveDuplicates(self,nums:List[int])->int:# Same hare and tortoise routine!
# check from the end, check each element
# if previous value is same as current value
# pop the current value
k=len(nums)-1foriinnums[::-1]:ifi==nums[k-1]andk>0:nums.pop(k)k-=1returnlen(nums)
classSolution:defmajorityElement(self,nums:List[int])->int:# This problem can be solved using hashmap and a counter
# but O(1) space? The majority element can be appearing at
# least 2 times, so we can keep a counter and ++ / -- it
# upon seeing it again. If majority element occures even times (>=2),
# we save it in result = i, if odd times (>=3), we don't update as it
# will have been updated during 2nd occurance
count=0result=0foriinnums:ifcount==0:result=iifi==result:count+=1else:count-=1returnresult
classSolution:defaddBinary(self,a:str,b:str)->str:x,y=int(a,2),int(b,2)whiley:answer=x^ycarry=(x&y)<<1x,y=answer,carry# to remove '0b' from answer, use [2:]
returnbin(x)[2:]
classSolution:defsingleNumber(self,nums:List[int])->int:# bit masking is about setting, clearing and toggling bits
# so don't get lost in problem. you are setting bits / making numbers
a,b=0,0mask=0foriinnums:# if a(current) == i(current), we saw it twice; once in i another in a
# we keep common bits using &
# we store bits using |
b|=a&i# if a(previous) == i(current) and vice versa, we toggle a accordingly
# we toggle using ^
a^=i# if an element appears three times, we will find it in b (twice)
# and a (third time) both. So in case something appears three times, we
# remove it both from a and b
mask=(a&b)a&=~maskb&=~mask# a holds only the number (bits) which was not repeated three times
returna
classSolution:defrangeBitwiseAnd(self,left:int,right:int)->int:# the trick of this problem is understanding common prefix
# a common prefix is needed because of how & resets a bit if any operand
# is a zero bit. So we can shift right until we hit 1s for both as long as
# left < right, and as we keep shifting right, we keep count. Finally we
# left shift 'left'/'right' to produce the actual prefix
count=0whileleft<right:left>>=1right>>=1count+=1returnright<<count