[leetcode-python3] 384. Shuffle an Array

384. Shuffle an Array - python3

Given an integer array nums, design an algorithm to randomly shuffle the array.

Implement the Solution class:

  • Solution(int[] nums) Initializes the object with the integer array nums.
  • int[] reset() Resets the array to its original configuration and returns it.
  • int[] shuffle() Returns a random shuffling of the array.

My Answer 1: Accepted (Runtime: 272 ms - 81.39% / Memory Usage: 19.4 MB - 79.79%)

class Solution:

    def __init__(self, nums: List[int]):
        self.nums = nums
        self.nums2 = nums

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

    def shuffle(self) -> List[int]:
        """
        Returns a random shuffling of the array.
        """
        shuffle = list(self.nums)
        random.shuffle(shuffle)
        return shuffle


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

nums2 를 만들어서 nums 를 복사해두고 reset 때 리턴

nums 를 리스트 안에 넣어준 후 suffle 돌림
-> 사실 이건 중복이 나와서 답을 참고..^^

새해 복 많이 받으세요~~

좋은 웹페이지 즐겨찾기