leetcode之Shuffle an Array

    xiaoxiao2025-03-29  8

    这题有了random模块的shuffle函数是非常简单的。只不过shuffle是直接改变list,需要先用一个中间的list进行替换,否则就改动了init里面的list。

    import random class Solution(object): def __init__(self, nums): """ :type nums: List[int] :type size: int """ self.list1 = nums def reset(self): """ Resets the array to its original configuration and return it. :rtype: List[int] """ return self.list1 def shuffle(self): """ Returns a random shuffling of the array. :rtype: List[int] """ list2 = self.list1[::] random.shuffle(list2) return list2
    转载请注明原文地址: https://ju.6miu.com/read-1297498.html
    最新回复(0)