Clean AC python version; Reservior sampling

  • 0
    import random
    class Solution(object):
        def __init__(self, nums):
            self.nums = nums
        def pick(self, target):
            count = 1
            res = None
            for index, num in enumerate(self.nums):
                if num == target:
                    if random.randint(1, count) == 1:
                        res = index
                    count += 1
            return res

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.