398. Random Pick Index
class Solution(object):
def __init__(self, nums):
"""
:type nums: List[int]
:type numsSize: int
"""
self.nums=nums
self.numsSize=len(nums)
def pick(self, target):
"""
:type target: int
:rtype: int
"""
index=-1
count=1
for i in range(self.numsSize):
if self.nums[i]==target:
if random.randint(0,count)==0:
index=i
count+=1
return index
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.pick(target)
本文标题:398. Random Pick Index
本文链接:https://www.haomeiwen.com/subject/auncvttx.html
网友评论