-
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathShuffleanArray.java
More file actions
31 lines (27 loc) · 706 Bytes
/
ShuffleanArray.java
File metadata and controls
31 lines (27 loc) · 706 Bytes
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
package problems.medium;
/**
* Created by sherxon on 1/2/17.
*/
public class ShuffleanArray {
int [] a;
int [] nums;
public ShuffleanArray(int[] nums) {
this.nums=nums;
a= new int[nums.length];
System.arraycopy(nums, 0, a, 0, nums.length);
}
/** 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() {
for(int i=0; i<a.length; i++){
int rand=(int) (Math.random()* (a.length));
int temp=a[i];
a[i]=a[rand];
a[rand]=temp;
}
return a;
}
}