-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0384 [M] Shuffle an Array
Code with Senpai edited this page May 20, 2022
·
1 revision
algorithm is called Fisher-Yates shuffle for every index i, pick a random index j from i to end, and swap(i, j)
class Solution:
def __init__(self, nums):
self.nums = nums[:]
def reset(self):
return self.nums
def shuffle(self):
self.copy = self.nums[:]
n = len(self.copy)
for i in range(n):
j = randint(i, n - 1)
self.copy[i], self.copy[j] = self.copy[j], self.copy[i]
return self.copy
footer