Scrambles an array with no repeating elements.

Example:

// Initialize the array with the numbers sets 1, 2, and 3. Int [] nums = {1, 2, 3}; Solution solution = new Solution(nums);

// scramble the array [1,2,3] and return the result. Any permutation [1,2,3] should return the same probability. solution.shuffle();

// reset the array to its initial state [1,2,3]. solution.reset();

// randomly returns the scrambled array [1,2,3]. solution.shuffle();

class Solution(object):

def __init__(self, nums):
    """
    :type nums: List[int]
    """
    self.nums = nums
    self.nums_old = nums
    
def reset(self):
    """
    Resets the array to its original configuration and return it.
    :rtype: List[int]
    """
    return self.nums_old
   

def shuffle(self):
    """
    Returns a random shuffling of the array.
    :rtype: List[int]
    """
    ssh = copy.deepcopy(self.nums)
    for i in range(0,len(self.nums)):
        sj = random.randint(0,len(ssh)-1)
        print i,sj
        ssh[i],ssh[sj] = ssh[sj],ssh[i]
    return ssh
Copy the code