当前位置:网站首页>Nc88 looking for the K largest
Nc88 looking for the K largest
2022-07-22 20:12: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;
}
}
边栏推荐
- Spark SQL: MySQL classic 50 questions (SQL version and spark version)
- Flutter开发(三十):Flutter简单app搭建
- Fluent 2 Advanced (IV): Based on navigator 2.0 packaging
- Shutter 2 Advanced (II): shutter air safety
- Flutter开发(三十二):Flutter屏幕适配
- Youboxun helps Shenzhen build a global "city of Hongmeng Oula"
- PyTorch学习——利用梯度下降法实现一元线性回归
- The problem of overwriting array when pushing
- location.replace和location.href区别
- pyplot. When using plot: userwarning: starting a Matplotlib GUI outside of the main thread will like fail
猜你喜欢
Opening soon | openatom openharmony sub forum of 2022 open atom global open source summit "interconnection of all things, enabling thousands of industries"
Kotlin学习一:变量、函数、条件语句与循环语句
使用Modelsim独立仿真Altera及Xilinx IP核
appstore 上传屏幕快照尺寸
Host Ping fails in VMware NAT mode virtual machine: different from most methods
NFT卡牌链游系统Dapp开发搭建
One master-slave replication of MySQL
Oracle 11g installs and starts EM based on centos7
Elastic Search 学习入门之核心概念(四)
Elastic Search 学习入门之插件安装(五)
随机推荐
Flutter 2进阶(四):基于Navigator 2.0封装
How to clear localstorage data after closing the browser
Fluent 2 Advanced (VI): use of callback functions
Kotlin learning II: classes and objects
location.replace和location.href区别
Flutter development (32): flutter screen adaptation
MySql分页
Development and construction of NFT card chain game system DAPP
【校招总结】【旧文回顾】百度实习收获美团网易小米华为远景offer
Elastic Search 学习入门之restful的高级查询操作(九)
leetcode 394. 字符串解码
Spark: graph
关闭浏览器 如何清除localStorage数据
【线上Case分析】一次慢查询优化及总结思考
Unknown AVD name [Pixel_2_API_30], use -list-avds to see valid li
[chat] essays after two years of work
Flutter 2进阶(六):回调函数的使用
If else optimization
Customization of development mode of NFT mining Dividend System
MySQL master-slave synchronization problem, repair the slave Library (transfer)