class Solution { int[] Arr; int[] Nature; public Solution(int[] nums) { Arr=nums; Nature= Arrays.copyOf(Arr, Arr.length); } /** Resets the array to its original configuration and return it. */ public int[] reset() { for(int i=0;i<Arr.length;i++) Arr[i]=Nature[i]; return Arrays.copyOf(Arr, Arr.length); } /** Returns a random shuffling of the array. */ public int[] shuffle() { for(int i=Arr.length-1;i>0;i--){ Random rand=new Random(); int t=rand.nextInt(i+1); //随机数生成范围要注意 int temp=Arr[t]; Arr[t]=Arr[i]; Arr[i]=temp; } return Arr; } }
/** * 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(); */
// class Solution { // int[] nums;
// public Solution(int[] nums) { // this.nums = nums; // } // /** 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[] result = Arrays.copyOf(nums, nums.length); // Random rand=new Random(); // for(int i = nums.length - 1; i > 0; i--) { // int n = rand.nextInt(i + 1); // // swap n and i // int tmp = result[n]; // result[n] = result[i]; // result[i] = tmp; // } // return result; // } // }
class Solution { public int countPrimes(int n) { int res=0; for(int i=1;i<n;i++) if(isPrime(i)) res++; return res; } public static boolean isPrime(int num) { if (num <= 3) { return num > 1; } // 不在6的倍数两侧的一定不是质数 if (num % 6 != 1 && num % 6 != 5) { returnfalse; } int sqrt = (int) Math.sqrt(num); for (int i = 5; i <= sqrt; i += 6) { if (num % i == 0 || num % (i + 2) == 0) { returnfalse; } } returntrue; }
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int count = 0; while (n != 0) { count++; n = n & (n - 1); } return count; } }