【easy】215. Kth Largest Element in an Array 第K大的数
class Solution {
public:
    int quicksort(vector<int>& nums, int start, int end, int k){
        int i = start;
        int j = end;
        int x = nums[i];
        while (i<j){            //快排核心…
            while (nums[j]<x && i<j)
                j--;
            if (i<j)
                nums[i++] = nums[j];
            while (nums[i]>x && i<j)
                i++;
            if (i<j)
                nums[j--]=nums[i];
        }
        nums[i] = x;
        if (i==k-) return x;
        else if (i>k-)         //出错的地方……………………
            return quicksort(nums,start,i-,k);
        else
            return quicksort(nums,i+,end,k);
    }
public:
    int findKthLargest(vector<int>& nums, int k) {
        int len = nums.size();
        //思路:快排,从大到小,放在第(K-1)处的就是第k大的
        int res = quicksort(nums,,len-,k);
        return res;
    }
};
【easy】215. Kth Largest Element in an Array 第K大的数的更多相关文章
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
		
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
 - 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
		
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
 - LN : leetcode 215 Kth Largest Element in an Array
		
lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...
 - LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解
		
题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...
 - 【LeetCode】215. Kth Largest Element in an Array (2 solutions)
		
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
 - 【刷题-LeetCode】215. Kth Largest Element in an Array
		
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
 - [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字
		
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
 - leetcode  215. Kth Largest Element in an Array
		
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
 - Java for LeetCode 215 Kth Largest Element in an Array
		
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
 
随机推荐
- Django中间件2
			
前戏 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上装饰 ...
 - mpvue-Vant Weapp踩坑记
			
微信开发者工具:开发.调试和模拟运行微信小程序的最核心的工具了,所以必须安装 # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 mpvue- ...
 - safari打开的页面数字识别变为蓝色
			
今天网页碰到一个很怪异的问题:app打开的一个网页样式是好的,但通过safari打开后数字的颜色变为蓝色,并且还变得可点击了! 原来safari总会把长串数字识别为电话号码,文字变成蓝色,点击还会弹出 ...
 - MySQL之 InnoDB记录结构(转自掘金小册 MySQL是怎样运行的,版权归作者所有!)
			
以下内容来自掘金小册 MySQL 是怎样运行的:从根儿上理解 MySQL 版权归原作者所有! 页是MySQL中磁盘和内存交互的基本单位,也是MySQL是管理存储空间的基本单位. 指定和修改行格式的语法 ...
 - Socket通信例子
			
Server端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...
 - CentOS_7升级系统内核
			
最近,在虚拟机中安装docker成功之后,尝试运行docker run hello-world时出现以下错误: $ sudo docker run hello-world Unable to find ...
 - Announcing the public preview of Azure Dev Spaces
			
Today, we are excited to announce the public preview of Azure Dev Spaces, a cloud-native development ...
 - python之MRO和垃圾回收机制
			
一.MOR 1.C3算法简介 为了解决原来基于深度优先搜索算法不满足本地优先级,和单调性的问题. python2.3版本之后不管是新式类还是经典类,查找继承顺序都采用C3算法 2.算法原理 C3算法的 ...
 - saltstack之自动化运维
			
引入 简介 saltstack是由thomas Hatch于2011年创建的一个开源项目,设计初衷是为了实现一个快速的远程执行系统. 早期运维人员会根据自己的生产环境来写特定脚本完成大量重复性工作,这 ...
 - Codeforces1102F Elongated Matrix 【状压DP】
			
题目分析: 这题瞎搞一个哈密尔顿路,对于起点不同的分开跑就可以过了. $O(n^3*2^n)$ #include<bits/stdc++.h> using namespace std; ; ...