Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate element must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:
- You must not modify the array (assume the array is read only).
- You must use only constant extra space.
- Your runtime complexity should be less than
O(n2)
.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Solution1:利用位图标记法,O(N)的时间空间复杂度
Solution2:若i!=j,nums[i]==nums[j],则会有x=nums[i]->i=x->x=nums[i]->i=x->x=nums[i]环出现,这样就是单链表的快慢指针思想了