Skip to content

Latest commit

 

History

History
55 lines (37 loc) · 1.23 KB

_384. Shuffle an Array.md

File metadata and controls

55 lines (37 loc) · 1.23 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 29, 2024

Last updated : June 29, 2024


Related Topics : Array, Math, Randomized

Acceptance Rate : 58.59 %


Solutions

Python

class Solution:

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

    def reset(self) -> List[int]:
        self.shuffled = self.nums.copy()
        return self.nums

    def shuffle(self) -> List[int]:
        indices = set(range(len(self.nums)))
        self.shuffled = []
        
        while indices :
            temp = random.choice(list(indices))
            self.shuffled.append(self.nums[temp])
            indices.remove(temp)
        return self.shuffled

        


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