Another python solution


  • -1
    J
    import random
    
    class Solution(object):
    
        def __init__(self, nums):
            """
            
            :type nums: List[int]
            :type numsSize: int
            """
            self.dic = {}
            for i in range(len(nums)):
                if nums[i] not in self.dic:
                    self.dic[nums[i]] = [i]
                else:
                    self.dic[nums[i]].append(i)
            
    
        def pick(self, target):
            """
            :type target: int
            :rtype: int
            """
            indices = self.dic[target]
            return random.choice(indices)
            
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.