当前位置:网站首页>剑指 Offer 40. 最小的k个数
剑指 Offer 40. 最小的k个数
2022-06-11 08:28:00 【zmm_mohua】
剑指 Offer 40. 最小的k个数
题目

代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> getLeastNumbers(vector<int>& arr, int k) {
vector<int> res;
if(k == 0){
return res;
}
sort(arr.begin(), arr.end());
for(int i = 0; i < k; i++){
res.push_back(arr[i]);
}
return res;
}
int main(){
int n, k;
cin>>n>>k;
vector<int> arr(n);
vector<int> res;
for(int i = 0; i < n; i++){
cin>>arr[i];
}
res = getLeastNumbers(arr, k);
for(int j = 0; j < res.size(); j++){
cout<<res[j]<<" ";
}
return 0;
}
边栏推荐
- 嵌入式软件面试问题总结
- 进程间的通信
- Planning tasks for continuous automated testing
- Typescript class and interface, class and generic, interface merging
- TRUNC in pytorch_ normal_ principle
- Asynchronous notification mechanism of character device driver
- Deep understanding of add in argparse module_ Argument parameters (such as action)
- Typescript null and undefined
- 进程控制:进程等待(回收子进程)
- In an activity, view postdelay will cause memory leakage, but will not affect the life cycle execution of the activity.
猜你喜欢

Difference between threadpooltaskexecutor and ThreadPoolExecutor

Oracle learning (I)

B+ super tree helps you know the underlying structure of MySQL

命名实体识别之CRF的实现方式

Qiao lerna: lerna auxiliary tool

Sign in system design: how to draw the sign in function

Introduction to database system experiment report answer Experiment 6: advanced query of data table

并查集是什么?你还在为其烦恼?其实就是一个连通图的问题,理解起来没有那么困难

uniapp 插件开发

What is concurrent search set? Are you still worried about it? In fact, it is a problem of connected graph, which is not so difficult to understand
随机推荐
Pycharm启动卡死,loading project
Web design and website planning assignment 12 online registration form
Process control: process waiting (recycling child processes)
Oracle learning (I)
js 中 Map 和 Set 的用法及区别
Training yolov4 CSP model using coco dataset
Introduction to guava cache usage
二、渲染 Camera 的数据
centos随笔03:centos8.2安装mysql
Typescript unknown type
Solution to the occurrence interval (space) of latex manual numbering documents
In an activity, view postdelay will cause memory leakage, but will not affect the life cycle execution of the activity.
(二)从架构设计角度分析AAC源码-我的LiveData
(taking pytorch as an example) a simple understanding of the regularization method of path (depth) -drop path
一些学习记录i=
Solve ('You must install pydot (`pip install pydot`) and install graphviz (see...) '‘ for plot_ model..
Redis cluster in Linux system
[programming development] markdown notes tutorial
B+超强树,带你知晓MySQL的底层是怎样的结构
Solve cannot import name 'multiheadattention' from 'tensorflow keras. layers‘