代码题(3)— 最小的k个数、数组中的第K个最大元素、前K个高频元素
1、题目:输入n个整数,找出其中最小的K个数。
例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。
快排思路(掌握):
class Solution {
public:
vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
vector<int> result;
if(input.empty() || k<= || input.size()<k)
return result;
int low = ;
int high = input.size() - ;
int index = partition(input, low, high);
while(index != k-)
{
if(index <= k- )
{
low = index + ;
index = partition(input, low, high);
}
else
{
high = index - ;
index = partition(input, low, high);
}
}
for(int i =; i<k; ++i)
{
result.push_back(input[i]);
}
return result;
}
int partition(vector<int> &input, int low, int high)
{
int temp = input[low];
while(low < high)
{
while(low<high && temp < input[high])
high--;
input[low] = input[high];
while(low<high && temp >=input[low])
low++;
input[high] = input[low];
}
input[low] = temp;
return low;
}
};
使用容器的方法:
class Solution {
public:
vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
vector<int> result;
if(input.empty() || k< || input.size()<k)
return result;
priority_queue<int> pq;
for(int i=;i<input.size();i++)
{
if(pq.size()<k)
{
pq.push(input[i]);
}
else
{
int maxVal=pq.top();
if(input[i]<maxVal)
{
pq.pop();
pq.push(input[i]);
}
}
}
while(!pq.empty())
{
result.push_back(pq.top());
pq.pop();
}
return result; }
};
2、215、数组中的第K个最大元素
题目:在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
示例 1:
输入:[3,2,1,5,6,4] 和
k = 2
输出: 5
示例 2:
输入:[3,2,3,1,2,4,5,5,6] 和
k = 4
输出: 4
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
if(nums.empty() || nums.size()<k || k<=)
return -;
vector<int> copyNums = nums;
int low = ;
int high = nums.size()-;
int index = partition(nums, low, high);
while(index != k-)
{ if(low<high && index < k-)
{
low = index+;
index = partition(nums,low, high);
}
else if(low<high && index > k-)
{
high = index-;
index = partition(nums, low,high); }
else
break;
}
for(int i=;i<nums.size();++i)
{
if(copyNums[i] == nums[index])
return copyNums[i];
}
return -; }
int partition(vector<int> &input, int low, int high)
{
int temp = input[low];
while(low < high)
{
while(low<high && temp > input[high])
high--;
input[low] = input[high];
while(low<high && temp <=input[low])
low++;
input[high] = input[low];
}
input[low] = temp;
return low;
}
};
3、347. 前K个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
示例 1:
输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
示例 2:
输入: nums = [1], k = 1
输出: [1]
说明:
- 你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。
- 你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> m;
priority_queue<pair<int,int>> q;
vector<int> res; for(int a:nums)
m[a]++;
for(auto it:m)
q.push({it.second, it.first});
for(int i=;i<k;++i)
{
res.push_back(q.top().second);
q.pop();
}
return res;
}
};
代码题(3)— 最小的k个数、数组中的第K个最大元素、前K个高频元素的更多相关文章
- 转:最小区间:k个有序的数组,找到最小区间使k个数组中每个数组至少有一个数在区间中
转:http://www.itmian4.com/thread-6504-1-1.html 最小区间原题 k个有序的数组,找到最小的区间范围使得这k个数组中,每个数组至少有一个数字在这个区间范围内.比 ...
- 窥探算法之美妙——寻找数组中最小的K个数&python中巧用最大堆
原文发表在我的博客主页,转载请注明出处 前言 不论是小算法或者大系统,堆一直是某种场景下程序员比较亲睐的数据结构,而在python中,由于数据结构的极其灵活性,list,tuple, dict在很多情 ...
- leecode刷题(1)-- 删除排序数组中的重复项
删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的 ...
- 【每天一题】LeetCode 0026. 删除排序数组中的重复项
开源地址:https://github.com/jiauzhang/algorithms 题目描述 /* * https://leetcode-cn.com/problems/remove-dupli ...
- #leetcode刷题之路26-删除排序数组中的重复项
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1: ...
- js从数组中删除指定值(不是指定位置)的元素
RT: js从数组中删除指定值的元素,注意是指定值,而不是指定位置. 比如数组{1,2,3,4,5},我要删除其中的元素3,但是这个3的位置我是不知道的,只知道要删除值为3的这一个元素,请问要怎么写? ...
- 对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素。 给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在数组中的位置(从0开始),若不存在该元素,返回-1。若该元素出现多次,请返回第一次出现的位置。
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- 剑指Offer:面试题30——最小的k个数(java实现)
问题描述: 输入n个整数,找出其中最小的k个数 思路1: 先排序,再取前k个 时间复杂度O(nlogn) 下面给出快排序的代码(基于下面Partition函数的方法) public void Quic ...
- 【算法】数组与矩阵问题——找到无序数组中最小的k个数
/** * 找到无序数组中最小的k个数 时间复杂度O(Nlogk) * 过程: * 1.一直维护一个有k个数的大根堆,这个堆代表目前选出来的k个最小的数 * 在堆里的k个元素中堆顶的元素是最小的k个数 ...
- 【Offer】[40] 【最小的K个数】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入n个整数,找出其中最小的k个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 牛客网刷题地 ...
随机推荐
- android 软键盘监听显示和隐藏
githup中找到:https://github.com/yescpu/KeyboardChangeListener import android.app.Activity; import andro ...
- PHP面试题总结
2017年5月15日19:20:26 1.请用最简单的语言告诉我PHP是什么? PHP全称:Hypertext Preprocessor,是一种用来开发动态网站的服务器脚本语言. 2. 面试题地址:h ...
- window下安装php7的memcache扩展
安装memcache:http://www.runoob.com/memcached/memcached-connection.html1.4.4 c:\memcached\memcached.exe ...
- iptables基础及samba配置举例
iptable基本概念 iptables防火墙包含两部分,即位于用户空间的iptables模块和位于内核空间netfilter模块.用户空间模块提供插入.修改和除去包过滤表中规则,内核模块进行实际的过 ...
- 让WebRTC支持H264编解码
近期实验了下怎样让WebRTC支持H264编码.记录下,供有须要的人參考. 说明一下,我是在 Ubuntu Server 14.04 下编译的 WebRTC ,使用 native(C++) api 开 ...
- java.sql.SQLException: 无法转换为内部表示 -〉java 查询oracle数据库返回错误信息
java.sql.SQLException: 无法转换为内部表示 Query: SELECT * FROM nontheasttycoon Parameters: [] at org.apac ...
- 【BZOJ4785】[Zjoi2017]树状数组 树套树(二维线段树)
[BZOJ4785][Zjoi2017]树状数组 Description 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历.那是一道基础的树状数组题.给出一 ...
- mysql存储过程之事务篇
mysql存储过程之事务篇 事务的四大特征: ACID:Atomic(原子性).Consistent(一致性).Isolated(独立性).Durable (持久性) MySQL的事务支持不是绑定在M ...
- ubuntu 17 编译BTCoin
一. 安装开发环境 sudo apt-get update sudo apt-get install build-essential libtool autotools-dev autoconf pk ...
- HTML/CSS/JS初始化
CSS <link type="text/css" href="http://www.mazey.cn/css/mazey-base.css" rel=& ...