-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path384_shuffleArray.py
43 lines (35 loc) · 1.01 KB
/
384_shuffleArray.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
40
41
42
43
class Solution:
def __init__(self, nums):
"""
:type nums: List[int]
"""
self.nums = nums
def reset(self):
"""
Resets the array to its original configuration and return it.
:rtype: List[int]
"""
return self.nums
def shuffle(self):
"""
Returns a random shuffling of the array.
:rtype: List[int]
"""
# no extra space:
# ans = self.nums[:]
# n = len(ans)
# for i in range(n-1, -1, -1):
# j = random.randint(0,i)
# ans[i], ans[j] = ans[j], ans[i]
# return ans
# auxiliary array
answer, temp = [], [] + self.nums
size = len(temp)
for _ in range(size):
index = random.randint(0, len(temp) - 1)
answer.append(temp.pop(index))
return answer
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()