当前位置:网站首页>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;
}
};
边栏推荐
- Problems in QT creator (additional unknown and error lines are listed in the debug output window)
- Use of laravel repository mode
- Lfi-rce without controllable documents
- Shell programming overview
- Invalid argument during startup: Failed to open the . conf file: redis-window
- 【BUUCTF】 Have Fun
- Project management - common English vocabulary I
- [extensive reading of papers] analyzing connections between user attributes, images, and text
- PHP common authentication / third-party methods
- V3 01_ Welcome
猜你喜欢

Learn about data kinship JSON format design from sqlflow JSON format

DiceCTF - knock-knock
![[extensive reading of papers] multimodal attribute extraction](/img/ec/546c107ac0d31deded7ca94fdf0e2d.jpg)
[extensive reading of papers] multimodal attribute extraction

@PathVariable

Using docker to manage MySQL services under Windows

1 figure to explain the difference and connection between nodejs and JS

Shell programming overview

KnightCTF WEB

Thinkphp5 log file contains trick

MySQL back to table query optimization
随机推荐
@PathVariable
2021-05-12
Querywrapper in mybaits plus
go channel && select
PHP excel export function encapsulation (based on phpexcel class)
Complete TCP forwarding server (kernel linked list + mutex)
For loop and promise to solve the problem of concurrent callback
Introduction to the construction and development of composer private warehouse
Uniapp upload image method
LIS error: this configuration section cannot be used in this path
Use PHP to delete the specified text content in the file
Problems in QT creator (additional unknown and error lines are listed in the debug output window)
DiceCTF - knock-knock
DefCamp Capture the Flag (D-CTF) 2021-22 web
DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
PHP reverses scenarios based on code and skillfully uses debug_ backtrace()
Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
V3 01_ Welcome
Go sync. WaitGroup
Thinkphp5 log file contains trick