HTMLify

LeetCode - Random Pick Index - Python
Views: 4 | Author: abh
from random import choice

class Solution:

    def __init__(self, nums: List[int]):
        self.nums = nums

    def pick(self, target: int) -> int:
        indexs = []
        for i, n in enumerate(self.nums):
            if n == target:
                indexs.append(i)
        return choice(indexs)


# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.pick(target)

Comments