This is the solution "truly" reset the random list step by step


  • 0
    K
    class Solution(object):
    
        def __init__(self, nums):
            """
            :type nums: List[int]
            """
            
            self.steps = []
            self.nums = nums
    
        def reset(self):
            """
            Resets the array to its original configuration and return it.
            :rtype: List[int]
            """
            if self.steps != []:
                for _ in xrange(len(self.nums)):
                    (s1, s2) = self.steps.pop()
                    self.nums[s1], self.nums[s2] = self.nums[s2], self.nums[s1]
            
            return self.nums
    
        def shuffle(self):
            """
            Returns a random shuffling of the array.
            :rtype: List[int]
            """
            
            nums = self.nums
            
            if len(nums) <= 1:
                return nums
                
            for i in range(len(nums)):
                rand = random.randrange(i, len(nums))
                nums[i], nums[rand] = nums[rand], nums[i]
                self.steps.append((i, rand))
            
            return nums
            
    # Your Solution object will be instantiated and called as such:
    # obj = Solution(nums)
    # param_1 = obj.reset()
    # param_2 = obj.shuffle()
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.