第k小的元素
利用快排思想,如果标志位大于k,则第k小的数字在左边,否则在右边。(程序是第k大的元素)
#include <iostream>
#include <vector>
using namespace std; //求向量中第k大的数字,从大到小进行排列 int q_sort(vector<int> &v, int low, int high)
{
int tmp = v[low]; while (low < high)
{
while (low<high && v[high] <= tmp)
--high;
v[low] = v[high]; while (low<high && v[low] >= tmp)
++low;
v[high] = v[low];
}
v[low] = tmp;
cout << low << endl;
for (auto &i : v)
cout << i << " ";
cout << endl;
return low;
}
int k_max(vector<int> &v, int low, int high,int k)
{
if (low >= high)
return v[low];
else
{
int mid = q_sort(v, low, high);
if (mid>k)
k_max(v, low, mid - ,k);
else if (mid < k)
k_max(v, mid + , high,k);
else
return v[mid];
} } int main()
{
vector<int> v = { , , , , , , , , , , };
int len = v.size();
int k_num=k_max(v, , len - , -); //此处为k-1,从大到小的排列,下标为k-1的数。
cout << k_num << endl;
return ;
}
第k小的元素的更多相关文章
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- 查找第k小的元素(O(n)递归解法)
今天分享一个小技巧,虽然是小技巧但是还是很有价值的,曾经是微软的面试题.题目是这样的,一个无序的数组让你找出第k小的元素,我当时看到这道题的时候也像很多人一样都是按普通的思维,先排序在去第K个,但是当 ...
- 求第k小的元素
用快排解决: 用快排,一趟排序后,根据基准值来缩小问题规模.基准值的下角标i 加1 表示了基准值在数组中第几小.如果k<i+1,那就在左半边找:如果k>i+1那就在右半边找.当基准值的下角 ...
- [Swift]LeetCode378. 有序矩阵中第K小的元素 | Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- 230. 二叉搜索树中第K小的元素
230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...
- leetcode 二叉搜索树中第K小的元素 python
二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元 ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- 230. Kth Smallest Element in a BST 找到bst中的第k小的元素
[抄题]: Given a binary search tree, write a function kthSmallest to find the kth smallest element in i ...
随机推荐
- LNMP组合环境
Mysql----二进制安装: 下载rpm包 创建MySQL用户: useradd -s /sbin/nologin mysql -M 这里遇到无法创建用户,提示:useradd: group mys ...
- jquery实现点击页面空白处,弹框消失
要求:点击1,弹框2显示,点击空白处,弹框2消失 $("#AddDevices"):按钮1 $(".addDeviceBox")弹框2 //点击添加设备弹框 $ ...
- Input子系统(二)【转】
转自:http://blog.chinaunix.net/uid-25047042-id-4192368.html 上一篇中粗略的分析了下input_dev,input_handle,input_ha ...
- /usr/bin/ld: warning: libavformat.so.57, needed by /home/camera.so, not found (try using -rpath or -rpath-link)
ffmpeg中,使用libavformt.so.57时,查找不到. 解决方案: 修改ld.so.conf文件,添加路径. sudo gedit /etc/ld.so.conf 在文件末尾添加路径 /u ...
- MySQL之CONCAT()的用法
mysql CONCAT()函数用于将多个字符串连接成一个字符串,是最重要的mysql函数之一,下面就将为您详细介绍mysql CONCAT()函数,供您参考 mysql CONCAT(str1,st ...
- 收藏一个可以学习javascript prototype的链接
https://github.com/mqyqingfeng/Blog/issues/2
- session前后台交互
下面是需求需要,不同的人群进来显示不同,但是还必须是这个走到哪都的存在,不能说点击别的页面,下面红框处的数据就没有了,必须是存在的并且登陆的类型不一样,显示的也不一样,开始的时候想到的是直接调用方法, ...
- css 生成图片添加的十字
<span class="add" title="继续上传"></span> .add { display: inline-block; ...
- asp.net core 的 razor pages 如何使用ajax调用后台方法
Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法. 当网页被写入浏览器时,基于服务器的代码能够创建动态内容. 在网页加载时,服务器在向浏览器返回页面 ...
- C#接口的简单创建及其用法
我初次接触接口(Interface),对接口的作用有点迷茫,C#接口中包含方法.属性.索引器和事件的声明,但常用的接口中一般就是方法和属性,然而接口中并没有方法的具体实现代码(不能提供任何成员实现), ...