This also lets us find the number of duplicates in the array, but gives a lot of restrictions. In fact, Leetcode seems to some restrictions on conditions, can not make judgments, we are still strict requirements of the good OH.
First of all, the first one can not change the array, in fact, this is to limit our sort, if the sort, and then iterate again quickly can find repeated numbers, time complexity O (NLOGN), Space complexity O (1);
Secondly, the space complexity O (1), which restricts us to use the hash lookup, because the hash time complexity is O (1), but the space wants O (n);
Secondly, the time complexity is less than O (n^2), which limits our brute force to solve, the circulation two times also may obtain the result, but the time complexity needs O (n^2);
Finally, it says that the number of repetitions in an array can be repeated more than two times, which limits our use of mathematical methods, that is, if only one number repeats two times, we can sum it up and subtract it to get the result.
Then, we look back at these four solutions, is obviously the third place can be optimized, n^2 can be reduced to NLOGN, commonly used is the second Division method. Find the middle number mid, and then iterate through the array, if it is greater than the number of the middle number, then the repetition of the number in the first half, we like the recursive thought, again in the first half of the execution, also if less than the description in the second half of the appearance. Notice here that we are compared to mid rather than nums[mid] because the number is fixed between 0~n numbers. Time complexity or O (NLOGN), Space complexity O (1)
class Solution (object): Def findduplicate (self, Nums): "": Type Nums:list[i
NT]: Rtype:int "" "left = 0 right = Len (nums)-1 while (left <= right): Mid = left + (right-left)/2 count = 0 to I in range (len (nums)): If Nums[i ] <= Mid:count + + = 1 if count > mid:right = mid-1 E Lse:left = mid + 1 return left
See Leetcode hint has two points, this has been not clear, see other people's blog, the original address, https://segmentfault.com/a/1190000003817671
The corresponding principle is mentioned here, if our sequence is not duplicated, for example, [1,2,3]. Then each subscript corresponds to a number, 0->1 1->2 2->3, we apply this mapping relationship, starting with subscript 0, finding 0 corresponding digits, and then finding the new number as the new subscript until the lower bound is exceeded. For example, our path in this example is 0->1->2->3.
So if the sequence is repetitive, [1,3,3,2], then there will be a repetition of the subscript corresponding number, 0->1 {1,2}->3 3->2, then our path will appear in the loop. 0->1->3->2->3->2 ..., there will be 3 2 This ring, then the beginning of the ring is the number of repeat. At this point we are using the Leetcode linked list Cycle | | method, you can see the blog in detail.
Class Solution (object):
def findduplicate (self, Nums): ""
: Type Nums:list[int]
: Rtype:int
""
slow = nums[0]
fast = Nums[nums[0] While
(slow!= fast):
slow = Nums[slow]
fast = nums[nums[fast]]< C10/>fast = 0 while
(Fast!= Slow):
slow = Nums[slow]
fast = Nums[fast] return
slow