Problem
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(nlogn) algorithm.
ExampleGiven [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5].
Note考察对Heap Sort, Quick Sort, Merge Sort的掌握。
Solution Merge Sortpublic class Solution { public void sortIntegers2(int[] A) { if (A.length <= 1) return; int[] B = new int[A.length]; sort(A, 0, A.length-1, B); } public void sort(int[] A, int start, int end, int[] B) { if (start >= end) return; int mid = start + (end - start) / 2; sort(A, start, mid, B); sort(A, mid+1, end, B); merge(A, start, mid, end, B); } public void merge(int[] A, int start, int mid, int end, int[] B) { int i = start, j = mid+1, index = start; while (i <= mid && right <= end) { if (A[i] < A[j]) B[index++] = A[i++]; else B[index++] = A[j++]; } while (i <= mid) B[index++] = A[i++]; while (j <= end) B[index++] = A[j++]; for (int k = start; k <= end; k++) A[k] = B[k]; } }Heap Sort
public class Solution { private static int[] a; private static int n; private static int left; private static int right; private static int largest; public void sortIntegers2(int[] A) { a = A; buildheap(a); for(int i=n;i>0;i--){ exchange(0, i); n=n-1; maxheap(a, 0); } } public static void buildheap(int []a){ n=a.length-1; for(int i=n/2;i>=0;i--){ maxheap(a,i); } } public static void maxheap(int[] a, int i){ left=2*i; right=2*i+1; if(left <= n && a[left] > a[i]){ largest=left; } else{ largest=i; } if(right <= n && a[right] > a[largest]){ largest=right; } if(largest!=i){ exchange(i,largest); maxheap(a, largest); } } public static void exchange(int i, int j){ int t=a[i]; a[i]=a[j]; a[j]=t; } }Quick Sort
public class Solution { public void sortIntegers2(int[] A) { if (A.length <= 1) return; quicksort(A, 0, A.length-1); } public void quicksort(int[] A, int start, int end) { if (start >= end) return; int mid = start+(end-start)/2; int pivot = A[mid], i = start, j = end; while (i <= j) { while (i <= j && A[i] < pivot) i++; while (i <= j && A[j] > pivot) j--; if (i <= j) { int temp = A[i]; A[i] = A[j]; A[j] = temp; i++; j--; } } quicksort(A, start, j); quicksort(A, i, end); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/64977.html
摘要:和唯一的不同是组合中不能存在重复的元素,因此,在递归时将初始位即可。 Combination Sum I Problem Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T...
Subsets Problem Given a set of distinct integers, return all possible subsets. Notice Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets. Example ...
摘要:遍历整个数组,用一个计数器,找出超过整个数组长度二分之一的那个数。规则是当前数等于,计数器加否则,计数器减。当的大小等于时,统计中所有的,并将所有对应的减,若被减为,就从中移除这对键值。 Majority Number I Problem Given an array of integers, the majority number is the number that occurs ...
摘要:建立动规数组,表示从起点处到达该点的可能性。循环结束后,数组对所有点作为终点的可能性都进行了赋值。和的不同在于找到最少的步数。此时的一定是满足条件的最小的,所以一定是最优解。 Jump Game Problem Given an array of non-negative integers, you are initially positioned at the first index...
摘要:因为取了队首就不能取队尾,所以对数组循环两次,一个从取到,一个从取到,比较两次循环后队尾元素,取较大者。注意,要先讨论当原数组位数小于时的情况。 Problem After robbing those houses on that street, the thief has found himself a new place for his thievery so that he wi...
阅读 2878·2021-11-24 09:39
阅读 3129·2021-11-19 10:00
阅读 1535·2021-10-27 14:17
阅读 1811·2021-10-14 09:43
阅读 960·2021-09-03 10:30
阅读 3412·2019-08-30 15:54
阅读 2728·2019-08-30 13:05
阅读 2005·2019-08-30 11:02