leetcode-抽样算法

it2023-11-12  70

384. 打乱数组

class Solution(object): 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] """ array=copy.copy(self.nums) for i in range(len(array)): random_num = random.randint(i,len(array)-1) array[i], array[random_num] = array[random_num], array[i] return array # Your Solution object will be instantiated and called as such: # obj = Solution(nums) # param_1 = obj.reset() # param_2 = obj.shuffle()

 

最新回复(0)