美文网首页
398. Random Pick Index [Medium]

398. Random Pick Index [Medium]

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-07-25 15:05 被阅读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]

相关文章

网友评论

      本文标题:398. Random Pick Index [Medium]

      本文链接:https://www.haomeiwen.com/subject/jsehrctx.html