LeetCode 二分查找第K大的数215. Kth Largest Element in an Array
来源:互联网 发布:pycharm和python 编辑:程序博客网 时间:2024/05/16 18:08
题目链接
https://leetcode.com/problems/kth-largest-element-in-an-array/#/description
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4]
and k = 2, return 5.
Note:
You may assume k is always valid, 1 ? k ? array's length.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
class Solution {public: int findKthLargest(vector<int>& nums, int k) //思想 第k大的数 在 数组中的下标的位置是 pos-1 判断k与pos-1 大小即可 { int left = 0; int right = nums.size() - 1;while(true){int pos = partition(nums,left,right);if(pos = k-1) return nums[pos];if(pos < k-1) left = pos + 1;if(pos > k-1) right = pos -1;} } int partition(vector<int>&v,int left,int right)//一次快速排序 { int pivot = v[left]; int low = left; int high = right; while(low < high)//此处超时 { while(low < high && v[high] >= pivot) high--; swap(v[low],v[high]); while(low < high && v[low] <= pivot) low++; swap(v[high],v[low]); } v[low] = pivot; return high; }};
阅读全文
0 0
- LeetCode 二分查找第K大的数215. Kth Largest Element in an Array
- 第二周:215. Kth Largest Element in an Array(找出第K大的数)
- 【LeetCode】215. Kth Largest Element in an Array找第K大的元素
- 【Top K 问题】[Leetcode-215] Kth Largest Element in an Array 数组中第K大的数
- Kth Largest Element in an Array 数组中的第K大的数
- 数组-Kth Largest Element in an Array(找出第K大的数)
- [LeetCode] Kth Largest Element in an Array (找出数组的第k大的元素)
- Leetcode 215 Kth Largest Element in an Array 数组中第k大的元素
- LeetCode 215. Kth Largest Element in an Array(第K大元素)
- leetcode解题之215. Kth Largest Element in an Array java 版(第k大的数字)
- leetcode 215:Kth Largest Element in an Array ( 分治-求第k小的数,第k大的数)
- 215. Kth Largest Element in an Array(数组中第k大的数字)
- LeetCode OJ 之 Kth Largest Element in an Array(数组中的第k大元素)
- 【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】
- 215. Kth Largest Element in an Array(数组中第k大元素)
- Leetcode 215 - Kth Largest Element in an Array(二分)
- 字符串算法——查找数组第K个最大值( Kth Largest Element in an Array)
- leetcode215---Kth Largest Element in an Array(第k大元素)
- 机器学习基石 3.3 Learning with Different Protocol
- Maven pom.xml配置详解
- 将ubuntu16.04中pyhton默认版本从2.7换为3.5
- slf4j log4j logback关系详解和相关用法
- java备份数据库四种方法(mysql,mssql数据库备份)
- LeetCode 二分查找第K大的数215. Kth Largest Element in an Array
- linux下编译C++程序
- SIP穿越NAT的rport机制
- ECMAScript 6 常用特性整理
- 记(Laravel)PDO 使用prepared statement 预处理LIMIT等字段遇到的坑。
- 开发者都应该使用的10个C++11特性
- 【字符串】查找一个字符串中第一个只出现两次的字符。比如:“abcdefabcdefabc”中第一个只出现两次为‘d’,要求时间复杂度为O(N),空间复杂度为O(1)
- Angular4+NodeJs+MySQL 入门-02 MySql操作类
- js将 一串数字1403149534转换为日期格式