这次是实操,上LeetCode找一道题来写,要在Topics: Array下,我选择了下面这道题
思路:
1.将原始数组深复制进类中的
self.origin
(避免干扰)
2.reset时返回原始数组
3.shuffle时先把self.origin深复制给一个临时变量tmp并对其调用random.shuffle(tmp)并返回
代码:
import random import copy class Solution: def __init__(self, nums): """ :type nums: List[int] """ self.origin = copy.copy(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] """ tmp = copy.copy(self.origin) random.shuffle(tmp) return tmp # Your Solution object will be instantiated and called as such: