美文网首页
398. Random Pick Index

398. Random Pick Index

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-22 12:26 被阅读0次
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