class Solution:
def __init__(self, nums: List[int]):
self.n = nums
self.og = list(nums) # clone in java
def reset(self) -> List[int]:
"""
Resets the array to its original configuration and return it.
"""
self.n = self.og
self.og = list(self.og)
return self.n
def shuffle(self) -> List[int]:
"""
Returns a random shuffling of the array.
"""
random.shuffle(self.n)
return self.n
Runtime: 272 ms, faster than 81.39% of Python3 online submissions for Shuffle an Array.
Memory Usage: 19.5 MB, less than 18.75% of Python3 online submissions for Shuffle an Array.
답지에는 어쩌구 알고리즘 쓰라는데... 그냥 shuffle 쓰면 안되는지ㅠ
def shuffle(self):
for i in range(len(self.array)):
swap_idx = random.randrange(i, len(self.array))
self.array[i], self.array[swap_idx] = self.array[swap_idx], self.array[i]
return self.array
def shuffle(self):
aux = list(self.array)
for idx in range(len(self.array)):
remove_idx = random.randrange(len(aux))
self.array[idx] = aux.pop(remove_idx)
return self.array
찐으로 랜덤 짜는 방법~