第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 ...
随机推荐
- Eclipse——在eclipse上安装Pydev插件实现python编程
介绍:2003年7月16日,以 Fabio Zadrozny 为首的三人开发小组在全球最大的开放源代码软件开发平台和仓库 SourceForge 上注册了一款新的项目,该项目实现了一个功能强大的 Ec ...
- Java(19)JDBC
一.使用jdbc的步骤 a.引入数据库厂商提供的驱动程序(引入jar包) b.记载驱动程序 Clss.forName("驱动程序类") c.获得连接 Connection con ...
- 小程序bindtap和cachetap的区别
<view bindtap='a'> 1 <view bindtap='b'> 2 <view bindtap='c'> 3 </view> </ ...
- 上传Jcenter
将自己封装的库发布分享到Jcenter
- numpy数组取每一列的数据
也可以运用到列表中,a原本是一个列表的嵌套,将a转为了数组进行此操作,可以取固定的值,这就是numpy的好处.
- MariaDB存储过程笔记
FECTH INTO 字段名不能与 CURSOR FOR 中select字段名一致,否则FETCH出的值均为空. DECLARE 字段名不能与 CURSOR FOR 中select语句内where条件 ...
- vue轮播图中间大两头小
<template> <div v-if="items.length" class="full-page-slide-wrapper"> ...
- python socket.error: [Errno 24] Too many open files
以openwrt AR9331开发板为例,socket连接到1019个就报错 “python socket.error: [Errno 24] Too many open files” 1.查看开发板 ...
- golang 关于 interface 的学习整理
Golang-interface(四 反射) go语言学习-reflect反射理解和简单使用 为什么在Go语言中要慎用interface{} golang将interface{}转换为struct g ...
- CocosCreator脚本中向依赖的组件赋值后, 被依赖的组件没有取到值的问题!
问题描述: 两个节点parent&child(其中都包含脚本组件), parent脚本组件依赖了child组件, 节点关系如下图: parent脚本内容如下: child脚本内容如下: 预览时 ...