2016阿里实习笔试:乱序保序输出

thinking:

(1)每次选择输出的数字是当前序列中最小的,记该数字下标为 index,数字为a

(2)检查index 之后的最小数为b

(3)如果index之前有小于b且大于a的数字出现,说明这些数字是乱序的数字,要和a一行保序输出


code:

int output_in_order(vector<int> &unordered_sequence)
{
    int next_output = 1, got_output;

    priority_queue<int, vector<int>, greater<int> > pq;

    for (size_t i = 0; i < unordered_sequence.size(); i++)
    {
        pq.push(unordered_sequence[i]);

        if (pq.top() < next_output)
        {
            cout << "duplicated index or index <= 0 in the sequence" << endl;
            return -1;
        }

        got_output = 0;
        while (!pq.empty() && pq.top() == next_output)
        {
            if
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值