剑指offer-最小的k个数

题目类型

数组 高级算法

题目描述

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

code

class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
        if(input.size()<k)
            return {};
        vector<int> ans;
        sort(input.begin(), input.end());
        for(int i=0; i<k; i++){
            ans.push_back(input[i]);
        }
        return ans;
    }
};
运行时间:3ms
占用内存:492k

Analyse

利用高级算法思想部分有待完善

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章