打乱一个没有重复元素的数组。
方案一:创建一个新列表记录原始列表
import random
class Solution(object):
def __init__(self, nums):
"""
:type nums: List[int]
"""
self.origin = nums[:]
self.output = nums
def reset(self):
"""
Resets the array to its original configuration and return it.
:rtype: List[int]
"""
return self.origin
def shuffle(self):
"""
Returns a random shuffling of the array.
:rtype: List[int]
"""
for i in range(len(self.output)):
j = random.randint(i, len(self.output) - 1)
self.output[i], self.output[j] = self.output[j], self.output[i]
return self.output