注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
本帖最后由 techDiscussion 于 2020-12-29 04:56 编辑
- public int findKthLargest(int[] A, int k) {
-
- k = A.length - k; // convert to index of k largest
-
- int L = 0;
- int R = A.length - 1; // 这把 R 当作 Pivot
-
- while (L <= R) {
-
- int i = L - 1;
-
- for (int j = L; j <= R - 1; j++){
-
- if (A[j] < A[R]) {
-
- i++;
- swap(A, i, j);
- }
- }
-
- ++i;
- swap(A, i, R);
- if (k < i) R = i - 1;
- else if (k > i) L = i + 1;
- else return A[i];
- }
- return -1; // k is invalid
- }
-
- public void swap(int[] nums, int i, int j){
-
- int tmp = nums[i];
- nums[i] = nums[j];
- nums[j] = tmp;
- }
复制代码
求大家分析下时间复杂度,感谢各位。
[/i][/i][/i] |