0%

Shuffle An Array

384. Shuffle an Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import copy
import random


class Solution(object):
def __init__(self, nums):
self.ori = copy.copy(nums)
self.nums = nums

def reset(self):
return self.ori

def shuffle(self):
for i in range(len(self.nums)):
pos = random.randrange(i, len(self.nums))
self.nums[i], self.nums[pos] = self.nums[pos], self.nums[i]
return self.nums