-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
52 lines (45 loc) · 1.22 KB
/
Solution.java
File metadata and controls
52 lines (45 loc) · 1.22 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
import java.util.Random;
/**
* Author : WindAsMe
* File : Solution.java
* Time : Create on 18-8-11
* Location : ../Home/JavaForLeeCode2/Solution.java
* Function : LeetCode No.384
*/
public class Solution {
private int[] nums = null;
private Random random = null;
public Solution(int[] nums) {
this.nums = nums.clone();
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() {
if(nums == null) return null;
int[] a = nums.clone();
for(int i = 1; i < nums.length; i++){
int j = random.nextInt(i + 1);
swap(a, i, j);
}
return a;
}
private void swap(int[] a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
int[] nums = {1,2,3};
System.out.println();
}
}
/**
* 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();
*/