leetcode-easy-design-384 Shuffle an Array

Posted rosyyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-easy-design-384 Shuffle an Array相关的知识,希望对你有一定的参考价值。

mycode

class Solution(object):

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        self.res = nums[:]
        self.shu = nums[:]
        

    def reset(self):
        """
        Resets the array to its original configuration and return it.
        :rtype: List[int]
        """
        return self.res

    def shuffle(self):
        """
        Returns a random shuffling of the array.
        :rtype: List[int]
        """
        import random
        self.shu = random.sample(self.res, len((self.res)))
        return self.shu

        

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

 

random.shuffle功能

import random
class Solution(object):

    def __init__(self, nums):
        self.nums = nums
    def reset(self):
        return self.nums
    def shuffle(self):
        new_nums = self.nums[:]
        random.shuffle(new_nums)
        return new_nums

 

以上是关于leetcode-easy-design-384 Shuffle an Array的主要内容,如果未能解决你的问题,请参考以下文章