Download Shuffle an Array.py
import random class Solution(object): def __init__(self, nums): """ :type nums: List[int] :type size: int """ self.origin = list(nums) self.curr = list(nums) self.size = len(nums) def reset(self): """ Resets the array to its original configuration and return it. :rtype: List[int] """ self.curr = list(self.origin) return self.curr def shuffle(self): """ Returns a random shuffling of the array. :rtype: List[int] """ for i in range(self.size): pos = random.randint(0, i) # swap i and pos self.curr[i], self.curr[pos] = self.curr[pos], self.curr[i] return self.curr # Your Solution object will be instantiated and called as such: # obj = Solution(nums) # param_1 = obj.reset() # param_2 = obj.shuffle()
Leetcode 384 Shuffle an Array problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.
Feedback is the most important part of any website.
If you have any query, suggestion or feedback, Please feel free to contact us.