398 Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.
Note: The array size can be very large. Solution that uses too much extra space will not pass the judge.
Example:
一看题我还以为要用hashmap来存起每个值的位置,然后pick的时候再从位置们里面random挑。看了答案原来要用水塘抽样。其实不是特别懂原理,感觉水塘抽样跟shuffle很像,都是框起前面一截,然后从哪一截里任意取一个。
Last updated