Simple one pass through the array while storing a 1 ON bit in the mask if a number between 1 and n is encountered. Return as soon as a duplicate is detected
def findDuplicate(self, nums):
:type nums: List[int]
mask = 0 for n in nums: if mask & (1 << n) != 0: return n mask |= 1 << n return 0
@feitong2 No, it's python... But when n gets larger, it is a problem having extra time and space complexity.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.