public String reverseStr(String s, int k) { char[] arr = s.toCharArray(); intlength= arr.length; for (inti=0; i < length; i = i + k * 2) { intj= i; int end; if (i + k > length) { end = length; } else { end = j + k; }
publicclassBubbleSort { //这个冒泡排序我们使用从小到大进行排序 publicint[] sort1(int []array){ long count=0; for (int i=0;i<array.length;i++){ for (int j=0;j<array.length-1;j++){ if (array[j]>array[j+1]){ int temp=array[j]; array[j]=array[j+1]; array[j+1]=temp;
//这种方式进行排序的过程中,需要经过n-1+n-2+....+1+0=(n-1)*n/2 publicint[] sort1(int []array){ long count=0; for (int i=0;i<array.length-1;i++){ int temp=array[i]; int index=i; for (int j=i;j<array.length-1;j++){ if (temp>array[j+1]){ temp=array[j+1]; index=j+1; } count++; } array[index]=array[i]; array[i]=temp; } System.out.println("一共经过了"+count+"次循环"); return array; }
long beginTime=System.currentTimeMillis(); selectionSort.sort1(array); long overTime=System.currentTimeMillis(); System.out.println("选择排序时间为"+(overTime-beginTime));
long beginTime1=System.currentTimeMillis(); bubbleSort.sort1(array1); long overTime1=System.currentTimeMillis(); System.out.println("冒泡排序时间为"+(overTime1-beginTime1));
publicstaticvoidmain(String[] args) { SelectionSort selectionSort=newSelectionSort(); BubbleSort bubbleSort=newBubbleSort(); InsertionSort insertionSort=newInsertionSort(); HillSort hillSort=newHillSort(); int []array=newint[100000]; int []array1=newint[100000]; int []array2=newint[100000]; int []array3=newint[100000]; for (int i=0;i<array.length;i++){ array[i]= (int) (Math.random()*(100000)); } for (int i=0;i<array1.length;i++){ array1[i]= (int) (Math.random()*(100000)); } for (int i=0;i<array2.length;i++){ array2[i]= (int) (Math.random()*(100000)); } for (int i=0;i<array3.length;i++){ array3[i]= (int) (Math.random()*(100000)); }
long beginTime=System.currentTimeMillis(); selectionSort.sort1(array); long overTime=System.currentTimeMillis(); System.out.println("选择排序时间为"+(overTime-beginTime));
long beginTime1=System.currentTimeMillis(); bubbleSort.sort1(array1); long overTime1=System.currentTimeMillis(); System.out.println("冒泡排序时间为"+(overTime1-beginTime1));
long beginTime2=System.currentTimeMillis(); insertionSort.sort1(array2); long overTime2=System.currentTimeMillis(); System.out.println("插入排序时间为"+(overTime2-beginTime2));
long beginTime3=System.currentTimeMillis(); hillSort.sort1(array3); long overTime3=System.currentTimeMillis(); System.out.println("希尔排序时间为"+(overTime3-beginTime3)); }
publicintgetRefValue(int[] array,int left,int right){ int center=(left+right)/2; if (array[left]<array[center]){ swapReferences(array,left,center); } if (array[right]<array[left]){ swapReferences(array,left,right); } if (array[right]<array[center]){ swapReferences(array,center,right); }
//制造临时数组 publicvoidmakeArray(int []array){ int []tempArr=newint[array.length]; judgeSort(array,tempArr,0,array.length-1); }
publicvoidsort(int []array,int []tempArr,int left,int center,int right){ //我们分析两个数组,发现这两个数组的边界为left~center-1和center~right int leftEnd=center-1; int tempLeft=left; int tempCenter=center; //定义开始存储数组的位置 int tempArrIndex=left; //当两个数组都在范围之内的时候 while (tempLeft<=leftEnd&&tempCenter<=right){ if (array[tempLeft]<=array[tempCenter]){ tempArr[tempArrIndex]=array[tempLeft]; tempLeft++; tempArrIndex++; }else { tempArr[tempArrIndex]=array[tempCenter]; tempCenter++; tempArrIndex++; } } //当结束的时候,我们将另外一个数组中的未赋值的元素全部加入其中 while (tempLeft<=leftEnd){ tempArr[tempArrIndex++]=array[tempLeft++]; } while (tempCenter<=right){ tempArr[tempArrIndex++]=array[tempCenter++]; }
//全部赋值完毕之后,将这个数放到其中 if (right + 1 - left >= 0) { System.arraycopy(tempArr, left, array, left, right + 1 - left); } System.out.println(Arrays.toString(array)); }
//这里使用递归进行运算,但是这样会造成栈空间的溢出,所以可以使用循环来代替 publicbooleanisHappy(int n) { ArrayList<Integer> arrayList=newArrayList<>(); int sum=0; int a=n; arrayList.add(n); while (n!=0){ int temp=n%10; n=n/10; sum=sum+temp*temp; } if (sum==1){ returntrue; } if (arrayList.contains(sum)){ returnfalse; } return isHappy(sum); }
publicbooleanisHappy(int n) { ArrayList<Integer> arrayList=newArrayList<>(); int sum=n; arrayList.add(n); while (sum!=1) { n=sum; sum=0; while (n != 0) { inttemp= n % 10; n = n / 10; sum = sum + temp * temp; } if (arrayList.contains(sum)){ returnfalse; }else { arrayList.add(sum); } } returntrue; }
publicint[] twoSum(int[] nums, int target) { HashMap<Integer ,Integer> hashMap=newHashMap<>(); for (int i=0;i<nums.length;i++){ hashMap.put(i,nums[i]); } int index1=0; int index2=0; for (int i=0;i<nums.length;i++){ int temp=target-nums[i]; hashMap.remove(i); if (hashMap.containsValue(temp)){ index1=i; index2=getKey(hashMap,temp); } } returnnewint[]{index1,index2}; }
publicintgetKey(HashMap<Integer,Integer> hashMap,Integer value){ for (Integer key: hashMap.keySet()){ if (hashMap.get(key).equals(value)){ return key; } } return -1; }
int count=0; Set<Integer> keySet1=hashMap1.keySet(); Set<Integer> keySet2=hashMap2.keySet(); for (Integer key:keySet1){ int temp= -key; if (keySet2.contains(temp)){ count=count+hashMap2.get(temp)*hashMap1.get(key); } }
publicstaticvoidmain(String[] args) { int []num1=newint[]{-1,-1}; int []num2=newint[]{-1,1}; int []num3=newint[]{-1,1}; int []num4=newint[]{1,-1}; Solution solution=newSolution(); solution.fourSumCount(num1,num2,num3,num4); } }
classSolution { publicintfourSumCount(int[] A, int[] B, int[] C, int[] D) { Map<Integer, Integer> countAB = newHashMap<Integer, Integer>(); for (int u : A) { for (int v : B) { countAB.put(u + v, countAB.getOrDefault(u + v, 0) + 1); } } intans=0; for (int u : C) { for (int v : D) { if (countAB.containsKey(-u - v)) { ans += countAB.get(-u - v); } } } return ans; }