Problem
Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree.
Given a unsorted array with integers, find the median of it.
A median is the middle number of the array after it is sorted.
If there are even numbers in the array, return the N/2-th number after sorted.
Example
Given [4, 5, 1, 2, 3], return 3.
Given [7, 9, 4, 5], return 5.
Challenge
O(n) time.
Note
理解快排。注意,作为pivot的元素在递归时要exclude出来。
Solution
Last as pivot
public class Solution {
public int median(int[] nums) {
return helper(nums, 0, nums.length-1, (nums.length-1)/2);
}
public int helper(int[] A, int start, int end, int k) {
int l = start, r = end;
int pivot = end, a = A[pivot];
while (l < r) {
while (l < r && A[l] < a) l++;
while (l < r && A[r] >= a) r--;
swap(A, l, r);
}
swap(A, l, end);
if (l == k) return A[l];
else if (l < k) return helper(A, l+1, end, k);
else return helper(A, start, l-1, k);
}
public void swap(int[] A, int l, int r) {
int temp = A[l];
A[l] = A[r];
A[r] = temp;
}
}
Sort Integers II
Merge sort
public class Solution {
/**
* @param A an integer array
* @return void
*/
public void sortIntegers2(int[] A) {
// Write your code here
if (A.length <= 1) return;
int[] B = new int[A.length];
sort(A, 0, A.length-1, B);
}
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);
}
void merge(int[] A, int start, int mid, int end, int[] B) {
int i = start, j = mid+1, index = start;
while (i <= mid && j <= end) {
if (A[i] < A[j]) B[index++] = A[i++];
else B[index++] = A[j++];
}
while (j <= end) B[index++] = A[j++];
while (i <= mid) B[index++] = A[i++];
for (int k = start; k <= end; k++) A[k] = B[k];
}
}
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 i = start, j = end;
int pivot = A[start+(end-start)/2];
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);
}
}
Heap Sort
public class Solution {
public void sortIntegers2(int[] A) {
buildheap(A);
int n = A.length-1;
for(int i = n; i > 0; i--){
exchange(A, 0, i);
n = n-1;
maxheap(A, 0, n);
}
}
public static void buildheap(int[] a){
int n = a.length-1;
for(int i = n/2; i>=0; i--){
maxheap(a, i, n);
}
}
public static void maxheap(int[] a, int i, int n){
int left = 2*i;
int right = 2*i+1;
int max = 0;
if(left <= n && a[left] > a[i]) max = left;
else max = i;
if(right <= n && a[right] > a[max]) max = right;
if(max != i){
exchange(a, i, max);
maxheap(a, max, n);
}
}
public static void exchange(int[] a, int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。