384. Shuffle an Array

384. Shuffle an Array

class Solution:

    def __init__(self, nums: List[int]):
        self.nums=nums
        self.nums1=nums[:]
    def reset(self) -> List[int]:
        return self.nums

    def shuffle(self) -> List[int]:
        n=len(self.nums1)
        for i in range(n):
            a,b=random.randint(0,n-1),random.randint(0,n-1)
            self.nums1[a],self.nums1[b]=self.nums1[b],self.nums1[a]
        return self.nums1


# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()

你可能感兴趣的:(leetcode)