1 Star 0 Fork 0

Neo_1001/LeetCode刷题记录

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LC_215_wrong.java 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
Neo_1001 提交于 2024-04-10 22:16 . first commit
package LeetCode;
import java.util.ArrayList;
public class LC_215_wrong {
static ArrayList<Integer> res = new ArrayList<>();
public static void main(String[] args) {
findKthLargest(new int[]{2,1}, 2);
}
public static int findKthLargest(int[] nums, int k) {
if (nums.length == 1) return nums[0];
quicksort(nums, 0, nums.length - 1, k);
int tmp = res.get(0);
res.clear();
return tmp;
}
public static void quicksort(int[] arr, int low, int high, int k) {
if (res.size() == 1) return;
if (low < high) {
int pos = part(arr, low, high, k);
quicksort(arr, low, pos - 1, k);
quicksort(arr, pos + 1, high, k);
}
}
public static int part(int[] arr, int low, int high, int k) {
int pivot = arr[low];
while (low < high) {
while (arr[high] >= pivot && low < high) high--;
arr[low] = arr[high];
while (arr[low] <= pivot && low < high) low++;
arr[high] = arr[low];
}
arr[low] = pivot;
if (low == arr.length - k) res.add(arr[low]);
return low;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neo1001/leet-code-practice-record.git
git@gitee.com:neo1001/leet-code-practice-record.git
neo1001
leet-code-practice-record
LeetCode刷题记录
master

搜索帮助