9. Math
获得随机数 Shuffle an array
public class Solution {
private int[] nums = null;
private Random random = null;
public Solution(int[] nums) {
this.nums = nums;
random = new Random();
}
/** Resets the array to its original configuration and return it. */
public int[] reset() {
return Arrays.copyOf(nums, nums.length);
}
/** Returns a random shuffling of the array. */
public int[] shuffle() {
int[] arr = Arrays.copyOf(nums, nums.length);
for(int i = 1; i < arr.length; i++){
int index = random.nextInt(i + 1);
swap(arr, i, index);
}
return arr;
}
private void swap(int[] nums, int a, int b){
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
}
}Count Primes
Reverse Integer
Last updated