-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path384.shuffle-an-array.py
39 lines (32 loc) · 1017 Bytes
/
384.shuffle-an-array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#
# @lc app=leetcode id=384 lang=python3
#
# [384] Shuffle an Array
#
# @lc code=start
class Solution:
# 356 ms, 16.41%. Time: O(N). Space: O(N)
def __init__(self, nums: List[int]):
self.original = nums[:]
self.nums = nums
def reset(self) -> List[int]:
return self.original
def shuffle(self) -> List[int]:
tmp = list(self.nums)
self.nums = []
while tmp:
index = random.randint(0, len(tmp) - 1)
tmp[-1], tmp[index] = tmp[index], tmp[-1]
self.nums.append(tmp.pop())
return self.nums
# article way: Fisher-Yates Algorithm
def shuffle(self) -> List[int]:
for i in range(len(self.nums)):
idx = random.randrange(i, len(self.nums))
self.nums[i], self.nums[idx] = self.nums[idx], self.nums[i]
return self.nums
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()
# @lc code=end