zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    统计字符
    两军交锋
    FatMouse' Trade
    A + B Problem II
    Number Sequence
    Max Sum
    类的设计
    类与对象
    面向对象思想
    第一个OC程序
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/14266038.html
Copyright © 2011-2022 走看看