In this Leetcode Shuffle an Array problem solution you have given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.

Implement the Solution class:

  1. Solution(int[] nums) Initializes the object with the integer array nums.
  2. int[] reset() Resets the array to its original configuration and returns it.
  3. int[] shuffle() Returns a random shuffling of the array.

Leetcode Shuffle an Array problem solution


Problem solution in Python.

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

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

    def shuffle(self):
        arr = self.original_array[:]
        for x in range(len(self.original_array)):
            n = random.randint(0, x)
            arr[x],arr[n] = arr[n], arr[x]
        return arr



Problem solution in Java.

public class Solution {
    public int[] ini;
    public Random rand;
    
    public Solution(int[] nums) {
        ini = nums;
        rand = new Random();
    }
    
    /** Resets the array to its original configuration and return it. */
    public int[] reset() {
        return ini;
    }
    
    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        boolean[] visited = new boolean[ini.length];
        int[] nums = new int[ini.length];
        int index = 0;
        while (index < nums.length) {
            int r = rand.nextInt(nums.length);
            if (visited[r] == false) {
                nums[index++] = ini[r];
                visited[r] = true;
            }
        }
        return nums;
    }
}


Problem solution in C++.

class Solution {
public:
    vector<int> array;
    vector<int> original;
    Solution(vector<int>& nums) : original(nums), array(nums) {}
    
    /** Resets the array to its original configuration and return it. */
    vector<int> reset() {
        return original;
    }
    
    /** Returns a random shuffling of the array. */
    vector<int> shuffle() {
        if(array.size() > 0){
            swap(array[rand() % array.size()],array[rand() % array.size()]);
        }
        return array;
    }
};


Problem solution in C.

typedef struct {
    int *original;
    int size;
} Solution;

Solution* solutionCreate(int* nums, int size) {
    Solution* obj=(Solution*)malloc(sizeof(Solution));
    obj->original=nums;
    obj->size=size;
    return obj;
}

/** Resets the array to its original configuration and return it. */
int* solutionReset(Solution* obj, int *returnSize) {
    *returnSize=obj->size;
    return obj->original;
}

/** Returns a random shuffling of the array. */
int* solutionShuffle(Solution* obj, int *returnSize) {
    *returnSize=obj->size;
    int *ret=(int*)calloc((*returnSize),sizeof(int));
    int *array=(int*)calloc((*returnSize),sizeof(int));
    int temp=0;
    int count=-1;
    for(int i=0;i<*returnSize;i++){
        temp=random()%((*returnSize)-i);
        for(int j=0;j<*returnSize;j++){
            if(array[j]==0){
                count++;
                if(count==temp){
                    array[j]=1;
                    count=-1;
                    ret[i]=obj->original[j];
                    break;
                }
            }
        }
    }
    return ret;
}

void solutionFree(Solution* obj) {
    free(obj->original);
    free(obj);
}