-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShuffleAnArray.java
More file actions
57 lines (46 loc) · 1.61 KB
/
Copy pathShuffleAnArray.java
File metadata and controls
57 lines (46 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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:
//
// Solution(int[] nums) Initializes the object with the integer array nums.
// - int[] reset() Resets the array to its original configuration and returns it.
// - int[] shuffle() Returns a random shuffling of the array.
// See: https://leetcode.com/problems/shuffle-an-array/
package leetcode.design;
import java.util.Arrays;
import java.util.Random;
/**
* Actually this is an implementation of Fisher-Yates algorithm
*/
public class ShuffleAnArray {
class Solution {
private int[] origin;
private int[] nums;
private Random rnd = new Random();
public Solution(int[] nums) {
origin = nums.clone();
this.nums = nums;
}
public int[] reset() {
return origin;
}
public int[] shuffle() {
// the initial solution was from start to end
// but in this case is easier to iterate backwards
for (int i = nums.length - 1; i >= 0; i--) {
swap(i, rnd.nextInt(i + 1));
}
return nums;
}
private void swap(int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
public static void main(String[] args) {
Solution sln = new ShuffleAnArray().new Solution(new int[] {1, 2, 3, 4});
System.out.println(Arrays.toString(sln.shuffle()));
}
}