第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 ...
随机推荐
- Coursera, Big Data 4, Machine Learning With Big Data (week 1/2)
Week 1 Machine Learning with Big Data KNime - GUI based Spark MLlib - inside Spark CRISP-DM Week 2, ...
- 初识springboot(傻瓜式教程)
初识springboot(傻瓜式教程) 项目所需的版本 IDEA 2018 maven 3.x jdk-1.8 IDEA创建spring-boot项目(maven方法) 1.创建一个maven工程 点 ...
- 开发一个项目之npm
npm (nodejs平台上写的js模块的管理工具 下载.互相依赖等) npm install 本地项目的node_modules文件夹 , -g npm config prefix 目录eg: ...
- tomcat用redis做session共享
在context.xml添加以下配置: <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerVa ...
- jetty切换tomcat中文乱码
项目中文在jetty下正常,换tomcat下出现乱码. 问题是web.xml中的encodingFilter不是第一个,要设置为第一个 <filter> <filter-name&g ...
- c++17 代码你能看懂吗?
------------------------------------------------------------------------------ #include <vector&g ...
- windows环境安装phantomjs和pyspider遇到的问题
1. 安装phantomjs 下载地址:http://phantomjs.org/download.html 解压后将phantomjs.exe文件放到python根目录 2.安装pyspider p ...
- FhqTreap的区间翻转
学 Fhq 就是为了尽量不去写某毒瘤数据结构,所以自然要来杠一杠某数据结构的经典操作:区间反转 听起来玄乎,但只需要一个小 trick 就行了:把原来的区间以下标作为权值建成 Treap , 这样整棵 ...
- java http post/get 服务端和客户端实现json传输
注:本文来源于<java http post/get 服务端和客户端实现json传输> 最近需要写http post接口所以学习下. 总的还是不难直接上源码! PostHttpClient ...
- 初识C语言(三)
C语言中的运算符 C语言中的运算就是对数据进行操作.处理的过程.运算符就是指定该运算的处理方式. C语言中的运算符: 算术运算符 赋值运算符 关系运算符 逻辑运算符 三目运算符 算数运算符 C语言中的 ...