当前位置:网站首页>The kth largest element in the sorted array
The kth largest element in the sorted array
2022-06-30 14:40:00 【Douglas_ LT】
A daily topic ing, Today is a day medium topic 215. Kth Largest Element in an Array
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
int len = nums.size();
int low = 0, high = len - 1,index;
while (true) {
index=partition(nums,low,high);
if (index == len - k) {
return nums[index];
}
else if(index>len-k){
high=index-1;
}
else{
low=index+1;
}
}
}
int partition(vector<int>&nums, int l, int r) {
int i = l, j = r, temp = nums[l];
while (i < j) {
while (i < j&&nums[j] >= temp) {
j--; }
if (i<j) {
nums[i] = nums[j];
}
while (i < j&&nums[i] <= temp) {
i++; }
if (i<j) {
nums[j] = nums[i];
}
}
nums[i] = temp;
return i;
}
};
边栏推荐
- Greedy interval problem (5)
- ES6 notes
- DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
- DefCamp Capture the Flag (D-CTF) 2021-22 web
- Summary of use of laravel DCAT admin
- Minimum covering substring of two pointers
- notepad正则删除关键词所在行
- Realize a simple LAN communication (similar to feiqiu)
- Pit used by go language array type
- 【BUUCTF】 EasySql
猜你喜欢
Att & CK red team evaluation field (I)
JS to realize simple lottery function
2021-05-12
2021-07-14 mybaitsplus
[buuctf] [geek challenge 2019] secret file
XSS challenge (6-10) more detailed answers
Laravel configures passport and returns token using JWT
Getting started with shell Basics
Fastcgi CGI shallow understanding
ctfshow nodejs
随机推荐
The JSON data returned from the control layer to JS has a "\" translator. How to remove it
Solution cannot use a scalar value as an array
Go common lock mutex and rwmutex
ES6 notes
jsPlumb. Deleteeveryconnection is not a function & jsplumb clear canvas jsplumb delete all nodes and all connections
Effect of shadow around the block after mouse over
About the problems encountered when using the timer class to stop with a button (why does the QPushButton (for the first time) need to be clicked twice to respond?)
Error on datetime when importing SQL file from MySQL
Add attributes to multimode
Three ways and differences of defining functions in JS
DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
PHP excel export function encapsulation (based on phpexcel class)
Logiciel de récupération de données easyrecovery15 téléchargement
PS cutting height 1px, Y-axis tiling background image problem
Is it troublesome for CITIC futures to open an account? Is it safe? How much is the handling charge for opening an account for futures? Can you offer a discount
Hbuilder most commonly used and full shortcut key set
Lihongyi machine learning 2020 homework summary
Not satisfied with markdown native code block style? Try this beautify code screenshot tool~~
Go language for loop multivariable use
Why does the folder appear open in another program