剑指offer---最小的k个数

class Solution
{
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k)
    {
        int in_size = input.size();
        for (int i = 0; i < in_size; ++i)
        {
            for (int j = i; j < in_size; ++j)
            {
                if (input[i]>input[j])
                {
                    swap(input[i], input[j]);
                }
            }
        }

        vector<int> result = {};
        if (k <= in_size)
        {
            for (int i = 0; i < k; ++i)
            {
                result.push_back(input[i]);
            }
            return result;
        }
        else
        {
            /*int lingdegeshu = (k - in_size);
            for (int i = 0; i < lingdegeshu; ++i)
            {
                result.push_back(0);
            }
            for (int j = 0; j < (in_size); ++j)
            {
                result.push_back(input[j]);
            }*/
            return result;
        }

        

    }
};

 

posted @ 2017-08-03 17:41  双马尾是老公的方向盘  阅读(110)  评论(0编辑  收藏  举报