Python | Leetcode Python题解之第384题打乱数组

题目:

Python | Leetcode Python题解之第384题打乱数组_第1张图片

题解:

class Solution:
    def __init__(self, nums: List[int]):
        self.nums = nums
        self.original = nums.copy()

    def reset(self) -> List[int]:
        self.nums = self.original.copy()
        return self.nums

    def shuffle(self) -> List[int]:
        for i in range(len(self.nums)):
            j = random.randrange(i, len(self.nums))
            self.nums[i], self.nums[j] = self.nums[j], self.nums[i]
        return self.nums

你可能感兴趣的:(分享,Python,Leetcode,题解)