# Java算法练习的重点是数组,字符和算术级数

class Solution { public int[] relativeSortArray(int[] arr1, int[] arr2) { int[] arr = new int[1001]; int[] ans = new int[arr1.length]; int index = 0; for(int i =0;ilt;arr1.length;i++){ arr[arr1[i]]+=1; } for(int i = 0;ilt;arr2.length;i++){ while(arr[arr2[i]]gt;0){ arr[arr2[i]]--; ans[index] = arr2[i]; index++; } } for(int i =0;ilt;arr.length;i++){ if(arr[i]!=0){ for(int j =0;jlt;arr[i];j++){ ans[index] = i; index++; } } } return ans; }}题目二

class Solution { public int findLucky(int[] arr) { int[] nums = new int[500]; for(int i =0;ilt;arr.length;i++){ nums[arr[i]]+=1; } int max = -1; for(int i = 1;ilt;nums.length;i++){ if(i == nums[i]){ max = Math.max(max,i); } } return max; }}题目三

class Solution { public int maxPower(String s) { if(s.length()==1) return 1; int left = 1; int max = Integer.MIN_VALUE; int con = 1; while(leftlt;s.length()){ if(s.charAt(left)==s.charAt(left-1)){ con++; max = Math.max(con,max); }else{ max = Math.max(con,max); con = 1; } left++; } return max; }}题目四

class Solution { public boolean canMakeArithmeticProgression(int[] arr) { Arrays.sort(arr); int num = arr[1]-arr[0]; for(int i = 1;ilt;arr.length;i++){ if(arr[i]-arr[i-1]==num){ continue; }else{ return false; } } return true; }}