题目:原题链接(中等)
标签:随机、蓄水池抽样
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
Ans 1 (Python) | 构造 = O ( 1 ); 随机= O ( N ) | O ( 1 ) | 216ms (47.03%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
class Solution:
def __init__(self, head: ListNode):
self.head = head
def getRandom(self) -> int:
now = 0
idx = 0
node = self.head
while node:
idx += 1
rand = random.randint(1, idx)
if rand == idx:
now = node.val
node = node.next
return now