最小的K个数

最小的K个数

题目描述

输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

未完, 待续, 好像设计堆排序

先排序在遍历, 此处使用插曲排序

class Solution {
public:void insertSort(vector<int> &vt) {int inner = 0;int outer = 0;for (outer = 1; outer < vt.size(); outer++) {int temp = vt[outer];inner = outer;while ((inner > 0) && (vt[inner-1] > temp)) {vt[inner] = vt[inner - 1];inner--;}vt[inner] = temp;}}vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {vector<int> ret;if ((0 > input.size()) || (0 >= k) || (k > input.size())) {return ret;}insertSort(input);for (int i = 0; i < k; i++) {ret.push_back(input[i]);}return ret;}
};

堆排序, 没看懂, 但感觉挺厉害的样子

class Solution {
public:vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {vector<int> result;if(input.size()==0||k==0||k>input.size()){return result;}for(int i=input.size()/2-1;i>=0;i--){//初始化堆adjustHeap(input,i,k);}int i=k;while(i<input.size()){if(input[0]>input[i]){int temp=input[i];input[i]=input[0];input[0]=temp;adjustHeap(input,0,k);i=k;}else {i++;}}for(int i=0;i<k;i++){result.push_back(input[i]);}return result;}void adjustHeap(vector<int>&input,int i,int length){//堆调整int child=i*2+1;if(child<length){if(child+1<length&&input[child+1]>input[child]){child=child+1;}if(input[child]>input[i]){int temp=input[i];input[i]=input[child];input[child]=temp;adjustHeap(input,child,length);}}}void heapSort(vector<int>&input,int length){//堆排序for(int i=length/2-1;i>=0;i--){//初始化堆adjustHeap(input,i,length);}for(int i=length-1;i>0;i--){int temp=input[i];input[i]=input[0];input[0]=temp;adjustHeap(input,0,i);}}};

转载于:https://www.cnblogs.com/hesper/p/10489606.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/248402.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

准备重新开始写了

工作很忙,而且前一段时间项目组由于方向和人员调整一直很动荡,所以就没有心情和时间来整理技术.准备重新开张了,好好写,争取每个月出一到两篇说得过去的文章.转载于:https://www.cnblogs.com/sun/archive/2008/06/12/1218220.html

Georgia and Bob POJ - 1704 阶梯Nim

$ \color{#0066ff}{ 题目描述 }$ Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, number the grids from left to right by 1, 2, 3, ..., and place N chessmen on different grids, as shown in the following figure for exampl…

Tomcat总结

Tomcat调优原理&#xff1a; 1、增加最大连接数&#xff08;增大值避免队列请求过多&#xff0c;导致响应缓慢&#xff09; 2、调整工作模式 Bio(BlockingI/O)&#xff1a;默认工作模式&#xff0c;阻塞式I/O操作&#xff0c;没有任何优化技术处理&#xff0c;性能比较低。Nio(…

Android中写文本文件的方法

下面是我在Android开发中&#xff0c;一个写文本文件的方法&#xff0c;代码如下&#xff1a; //将字符串写入到文本文件中 public static void WriteTxtFile(String strcontent,String strFilePath) { //每次写入时&#xff0c;都换行写 String strConten…

前端笔记-jquery

jquery简介 兼容性强,轻量级库,js的框架,国外的大神写好我们只要调用就好了,jquery可以把js写的更加简单 jquery使用 <script srcjquery-x.x.x.js></script> 引入文件就行了 jquery语法 $(selector).action() jquery选择器 1.基本选择器 $("*") $(&quo…

JVM的监控工具之jstack

参考博客&#xff1a;https://www.jianshu.com/p/213710fb9e40 jstack&#xff08;Stack Trace for Java&#xff09;命令用于生成虚拟机当前时刻的线程快照&#xff08;一般称为threaddump或者javacore文件&#xff09;。线程快照就是当前虚拟机内每一条线程正在执行的方法堆栈…

liunx驱动----异步通知

查询&#xff1a;消耗资源 中断&#xff1a;read 一直要去读poll &#xff1a;指定起始时间异步通知signal 测试程序include <stdio.h> include <signal.h>void my_signal(int signum) {static unsigned int cnt;printf("signum %d, %d timer\n",signum…

面试官: 用css实现android系统的loading动画

源码: github.com/any86/any-u… ios/android web常用的loading图标有2种, 一种是ios的"菊花", 一种是android的"环". 今天我们用svg实现android的"环"动画, 下节课实现ios的"菊花". 注意: gif帧数少的原因, 实际动画效果是很平滑的.d…

2018-06-29 西游记主题Python入门示例尝试-数据结构 5.1-5.1.2

(见前: 中文代码示例视频演示Python入门第五章 数据结构 仍然基于官方文档, 欢迎建议(尤其是如何取材). 5. Data Structures - More on Lists 列表详述 >>> 人物 [佛, 妖, 凡人, 菩萨, 妖, 凡人] >>> 人物.count(妖) 2 >>> 人物.count(圣人) 0 >…

Niginx 集群负载均衡策略

Niginx 集群负载均衡策略 所需物料 1.Nginx服务 步骤略 本人 nginx version: nginx/1.16.0 2.Java Servlet 测试项目 新建java web 项目&#xff0c;项目名称为&#xff1a;tt import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annot…

C#循环给多个控件赋值

需要给 多个 文本框重新赋值 1 textBox1.Text"ss"; 2 3 textBox2.Text"ss"; 4 5 textBox999.Text"ss"; 6 7 textBox99999.Text"ss"; 这样太麻烦&#xff0c;控件过多不方便写 通过遍历 一次性赋值&#xff0c;再多也不怕了 将所有T…

记号一次更换IBM X3650M4主板后RAID无法启动的解决

https://wenku.baidu.com/view/9d503ef367ec102de2bd89d7.html 强烈感谢上面分享文档的大侠&#xff01;&#xff01; 1、更换主板后&#xff0c;linux系统&#xff0c;无法加载引导。需要设置主板的启动项 2、选择boot manager&#xff0c;进到下面的画面 3、选择add boot opt…

关于REST API设计的一些小经验

版权声明&#xff1a;转载时请以超链接形式标明文章原始出处和作者信息及本声明http://phoenixtoday.blogbus.com/logs/45855234.html 最近小组里有一些关于REST API设计的讨论&#xff0c;有些收获&#xff0c;打算在这里写一下。通常来讲设计第一个版本的REST API并不难&…

1037 在霍格沃茨找零钱

题目传送门&#xff1a;https://pintia.cn/problem-sets/994805260223102976/problems/994805284923359232 题解&#xff1a; #include<iostream> using namespace std;int main() {int Galleon1, Sickle1, Knut1, Galleon2, Sickle2, Knut2;char c;cin >> Galleon…

我对创业和管理的一些看法

创业&#xff0c;对于刚工作的人&#xff0c;会比较兴奋&#xff0c;因为创业充满想象力&#xff1b;对于工作几年的人&#xff0c;会比较向往&#xff0c;因为压抑得太久。其实&#xff0c;创业和就业一样&#xff0c;只是实现自己人生价值的两种方式&#xff0c;关键是心态问…

关于Anaconda的环境和包管理

Anaconda相对于原生python解释器具有更好的包管理功能&#xff0c;它有一个env文件夹&#xff0c;里面包含所要管理的所有环境&#xff1b;日常操作时我们可能会使用pytorch、Tensorflow等多个环境&#xff0c;由于每个环境对Python的包的兼容性都不一样&#xff0c;所以我们可…

WCF basicHttpBinding之Message Security Mode

原创地址&#xff1a;http://www.cnblogs.com/jfzhu/p/4067873.html 转载请注明出处 前面的文章《WCF Security基本概念》介绍了WCF的security mode&#xff0c;简单说Transport是transport级别上的加密&am…

战略游戏

题目描述 Bob喜欢玩电脑游戏&#xff0c;特别是战略游戏。但是他经常无法找到快速玩过游戏的办法。现在他有个问题。 他要建立一个古城堡&#xff0c;城堡中的路形成一棵树。他要在这棵树的结点上放置最少数目的士兵&#xff0c;使得这些士兵能了望到所有的路。 注意&#xff0…

Vue语法学习第三课——计算属性

模板内的表达式非常便利&#xff0c;但是设计它们的初衷是用于简单运算的。在模板中放入太多的逻辑会让模板过重且难以维护。对于任何复杂逻辑&#xff0c;都应当使用计算属性。 <div id"example"><p>original message : "{{message}}"</p&…

云计算学习资料分享:type查看命令

type 查看命令类型&#xff0c;例如该命令是alias&#xff0c;还是内置命令&#xff0c;还是某个文件&#xff0c;还是关键字 哪种神仙&#xff1a;天上还是地上&#xff0c;还是水里游的 [roottianyun ~]# type ll ll is aliased to ls -l --colorauto [roottianyun ~]# type …