当前位置:网站首页>NC88 寻找第K大
NC88 寻找第K大
2022-07-22 10:17:00 【henujolly】
import java.util.*;
public class Finder {
public int findKth(int[] a, int n, int K) {
// write code here
return findK(a, 0, n-1, K);
}
public static int partition(int[] arr, int left, int right) {
int pivot = arr[left];
while (left < right) {
while (left < right && arr[right] <= pivot) {
right--;
}
arr[left] = arr[right];
while (left < right && arr[left] >= pivot) {
left++;
}
arr[right] = arr[left];
}
arr[left] = pivot;
return left;
}
public static int findK(int[] arr, int left, int right, int k) {
if (left <= right) {
int pivot = partition(arr, left, right);
if (pivot == k - 1) {
return arr[pivot];
} else if (pivot < k - 1) {
return findK(arr, pivot + 1, right, k);
} else {
return findK(arr, left, pivot - 1, k);
}
}
return -1;
}
}
边栏推荐
猜你喜欢
oracle用一条sql查出哪些数据不在某个表里
pyinstaller打包scrapy
作为初学者,我表示不想使用ESLint
Pyinstaller packaging scene
Data model design of newsql database
NewSQL数据库数据模型设计
JVM-VisualVM:多合-故障处理工具
Neo4j example: figure relationship map of the annals of the Three Kingdoms
It is found that the MariaDB database is 12 hours late, and the xxljob scheduled task scheduling is abnormal
How can ZABBIX customize MySQL monitoring items and trigger alarms
随机推荐
Why choose b+ tree for index
ZABBIX server downtime report "\u zbx\u mem\u malloc(): out of memory (requested 96 bytes)"
Datablau5.0 data asset management product series heavy release
-Bash: ifconfig: command not found
nacos权限与数据库
Spark RDD的依赖于DAG的工作原理
JVM jhat (virtual machine heap dump snapshot analysis tool)
ES6新特性分享(四)
Implementation of bytecode technology in dynamic proxy
数据架构与数据库建模
C#正则表达式提取指定词语出现的索引位置
Dnsmasq installation and configuration
Typora下载和简单使用教程
mysql 主从同步出问题,重新修复从库(转)
Setting proxy method in PHP curl request
低代码和无代码有什么区别?
-bash: ifconfig: 未找到命令
zabbix怎样自定义mysql监控项并触发告警
文献记录(part109)--Self-Representation Based Unsupervised Exemplar Selection in a Union of Subspaces
shell script “<< EOF”我的用途和遇到的问题