码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-easy-design-384 Shuffle an Array

时间:2019-06-11 15:02:05      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:list   sign   new   call   pre   elf   iat   ini   col   

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

标签:list   sign   new   call   pre   elf   iat   ini   col   

原文地址:https://www.cnblogs.com/rosyYY/p/11003513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!