题解 | #寻找第K大#
寻找第K大
http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf
import java.util.*; public class Solution { public int findKth(int[] a, int n, int K) { // write code here quick(a, 0, a.length - 1); return a[n-K]; } public static void quick(int[] arr, int l, int r) { if (l >= r) return; int i = l, j = r; while (i < j) { while (i < j && arr[j] >= arr[l]) j--; while (i < j && arr[i] <= arr[l]) i++; swap(arr, i, j); } swap(arr, i, l); quick(arr, l, i - 1); quick(arr, i + 1, r); } public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }