398. Random Pick Index [Medium]

2019-07-25  本文已影响0人  一个想当大佬的菜鸡

398. Random Pick Index

感觉没什么意义啊这题,难道是我解题思路有问题?

import random
class Solution(object):

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        self.nums = nums
                
    def pick(self, target):
        """
        :type target: int
        :rtype: int
        """
        count = 0
        res = []
        for i, v in enumerate(self.nums):
            if v == target:
                count += 1
                res.append(i)
        index = random.randrange(count)
        return res[index]
上一篇 下一篇

猜你喜欢

热点阅读