-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path384.shuffle-an-array.java
More file actions
58 lines (51 loc) · 1.39 KB
/
384.shuffle-an-array.java
File metadata and controls
58 lines (51 loc) · 1.39 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
/*
* @lc app=leetcode id=384 lang=java
*
* [384] Shuffle an Array
*/
// @lc code=start
/*
"inside-out" version of the shuffle
https://en.wikipedia.org/wiki/Fisher–Yates_shuffle#The_"inside-out"_algorithm
To initialize an array a of n elements to a randomly shuffled copy of source, both 0-based:
for i from 0 to n − 1 do
j ← random integer such that 0 ≤ j ≤ i
if j ≠ i
a[i] ← a[j]
a[j] ← source[i]
*/
class Solution {
int[] nums;
Random random;
public Solution(int[] nums) {
this.nums = nums;
random = 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() {
int[] rand = new int[nums.length];
for (int i = 0; i < nums.length; ++i) {
int j = random.nextInt(i + 1);
rand[i] = rand[j];
rand[j] = nums[i];
}
return rand;
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int[] param_1 = obj.reset();
* int[] param_2 = obj.shuffle();
*/
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int[] param_1 = obj.reset();
* int[] param_2 = obj.shuffle();
*/
// @lc code=end