HTMLify
LeetCode - Random Pick Index - Python
Views: 372 | Author: abh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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) |