美文网首页
01_Shuffle an Array

01_Shuffle an Array

作者: butters001 | 来源:发表于2019-11-09 13:01 被阅读0次
# 看了其他语言的解题代码 人生苦短 用我python 😂😂😂😂😂😂
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]
        """
        import random
        nums = self.nums[:]
        random.shuffle(nums)
        return nums


# random.sample()不用自定义新的数组
class Solution2(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]
        """
        import random
        return random.sample(self.nums, len(self.nums))

# Your Solution object will be instantiated and called as such:
nums = [1,2,3]
obj = Solution2(nums)
param_1 = obj.reset()
print(nums)
param_2 = obj.shuffle()
print(param_2)

相关文章

网友评论

      本文标题:01_Shuffle an Array

      本文链接:https://www.haomeiwen.com/subject/jotnbctx.html