137-打乱数组返回

给你一个整数数组 nums ,设计算法来打乱一个没有重复元素的数组。(这个我写的,但是我感觉用别人的随机算法是挺好,但是就是不知道原理)
class Solution(object):

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        self.nums = nums
        self.new_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]
        """
        self.new_nums = random.sample(self.nums, len(self.nums))
        return self.new_nums
posted @ 2021-01-12 11:15  楠海  阅读(65)  评论(0编辑  收藏  举报