## Friday, August 12, 2016

### LeetCode 384 - Shuffle an Array

https://www.hrwhisper.me/leetcode-shuffle-array/
Shuffle a set of numbers without duplicates.
Example:

```// Init an array with set 1, 2, and 3.
int[] nums = {1,2,3};
Solution solution = new Solution(nums);

// Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
solution.shuffle();

// Resets the array back to its original configuration [1,2,3].
solution.reset();

// Returns the random shuffling of array [1,2,3].
solution.shuffle();
```

private int[] nums;
private int[] output;
private Random random;

public Solution(int[] nums) {
this.nums = nums;
this.output = Arrays.copyOf(nums,nums.length);
this.random = new Random();
}

/** Resets the array to its original configuration and return it. */
public int[] reset() {
return this.output = Arrays.copyOf(nums,nums.length);
}

/** Returns a random shuffling of the array. */
public int[] shuffle() {
int n = output.length;
for (int i = 0; i < n; i++) {
int _id = random.nextInt(n-i);
int temp = output[i];
output[i] = output[i+_id];
output[i+_id] = temp;
}
return output;
}

http://www.learn4master.com/interview-questions/leetcode/leetcode-shuffle-an-array-java
int[] origin;
int[] res;
Random r;
public Solution(int[] nums) {
origin = nums;
res = Arrays.copyOf(nums, nums.length);
r = new Random();
}

/** Resets the array to its original configuration and return it. */
public int[] reset() {
res = Arrays.copyOf(origin, origin.length);
return res;
}

/** Returns a random shuffling of the array. */
public int[] shuffle() {
int right = res.length - 1;
int j;

while(right > 0) {
j = r.nextInt(right + 1);
swap(res, j, right);
right--;
}
return res;
}

void swap(int[] A, int i, int j) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}

http://www.guoting.org/leetcode/leetcode-384-shuffle-an-array/
```    private int[] nums;
private Random rand;

public Solution(int[] nums){
this.nums=nums;
this.rand=new Random();
}

/** Resets the array to its original configuration and return it. */
public int[] reset(){
return nums;
}

/** Returns a random shuffling of the array. */
public int[] shuffle(){
if(nums==null) return null;
int[] arr=nums.clone();
for(int i=1;i<arr.length;i++) {
int j=rand.nextInt(i+1);
swap(arr,i,j);
}
return arr;
}

private void swap(int[] arr,int i,int j){
int tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}```
http://www.cnblogs.com/grandyang/p/5783392.html

```    Solution(vector<int> nums): v(nums) {}

/** Resets the array to its original configuration and return it. */
vector<int> reset() {
return v;
}

/** Returns a random shuffling of the array. */
vector<int> shuffle() {
vector<int> res = v;
for (int i = 0; i < res.size(); ++i) {
int t = rand() % res.size();
swap(res[i], res[t]);
}
return res;
}```