- 题目
- 哈希表索引
题目
提示:参考力扣官方题解
哈希表索引时间复杂度 O(n)
空间复杂度O(n)
from collections import defaultdict
from secrets import choice
class Solution:
def __init__(self, nums: List[int]):
self.pos = defaultdict(list)
for i, num in enumerate(nums):
self.pos[num].append(i)
def pick(self, target: int) -> int:
return choice(self.pos[target])
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.pick(target)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)