1/19/2021

[LeetCode] 384. Shuffle an Array

Problem : https://leetcode.com/problems/shuffle-an-array/



class Solution:

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

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

    def shuffle(self) -> List[int]:
        """
        Returns a random shuffling of the array.
        """
        
        shuffled = self.nums[:]
        N = len(shuffled)
        
        for i in range(N):
            j = random.randint(i, N-1)
            shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
            
        return shuffled


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

No comments:

Post a Comment