leetcode 215. Kth Largest Element in an Array
2017-01-09 00:42
330 查看
class Solution { public: int partition(vector<int>& nums, int left, int right) { int pivot = nums[left]; int l = left + 1, r = right; while (l <= r) { if (nums[l] < pivot && nums[r] > pivot) swap(nums[l++], nums[r--]); if (nums[l] >= pivot) l++; if (nums[r] <= pivot) r--; } swap(nums[left], nums[r]); return r; } int findKthLargest(vector<int>& nums, int k) { int left = 0, right = nums.size() - 1; while (true) { int pos = partition(nums, left, right); if (pos == k - 1) return nums[pos]; if (pos > k - 1) right = pos - 1; else left = pos + 1; } } };
相关文章推荐
- LeetCode 215. Kth Largest Element in an Array(第K大元素)
- [leetcode] 215. Kth Largest Element in an Array
- leetcode 215. Kth Largest Element in an Array
- [leetcode] 215. Kth Largest Element in an Array
- leetcode刷题之旅——215. Kth Largest Element in an Array
- 215. Kth Largest Element in an Array LeetCode
- leetcode 215. Kth Largest Element in an Array
- leetcode 215. Kth Largest Element in an Array | Java最短代码实现
- Leetcode 215. Kth Largest Element in an Array[medium]
- 【leetcode】215. Kth Largest Element in an Array
- [leetcode]215. Kth Largest Element in an Array
- Leetcode 215. Kth Largest Element in an Array
- 【LeetCode】215. Kth Largest Element in an Array,基于Java和C++的解法
- LeetCode 215. Kth Largest Element in an Array
- [LeetCode]215. Kth Largest Element in an Array
- leetcode 215. Kth Largest Element in an Array
- [leetcode] 215. Kth Largest Element in an Array 解题报告
- LeetCode215. Kth Largest Element in an Array
- Leetcode 215. Kth Largest Element in an Array
- 【LeetCode】215. Kth Largest Element in an Array (Medium)