其他分享
首页 > 其他分享> > 寒武纪二面:

寒武纪二面:

作者:互联网

2020.08.05

1、多线程

2、IPC、共享内存

3、bind

4、合并n个有序链表 (力扣原题 使用最小堆会快一些)

 

#include <queue>

using namespace std;

 

struct ListNode {

    int val;

    ListNode* next;

    ListNode(int x) : val(x), next(NULL) {}

};

 

class Solution {

    struct cmp {

        bool operator() (ListNode* a, ListNode* b) {

            return a->val > b->val;

        }

    };

    

    ListNode* mergeKLists(vector<ListNode*>& lists) {

        priority_queue<ListNode*, vector<ListNode*>, cmp> pri;

        for (auto &elem : lists) {

            if (elem) {

                pri.push(elem);

            }

        }

        

        ListNode* head = new ListNode(0);

        ListNode* pre = head;

        while(!pri.empty()) {

            pre->next = pri.top();

            ListNode* tmp = pri.top()->next;

            pri.pop();

            if (tmp) {

                pri.push(tmp);

            }

            pre = pre->next;

        }

        

        pre = head->next;

        delete head;

        

        return pre;

    };

};

标签:pre,head,ListNode,val,pri,next,寒武纪,二面
来源: https://www.cnblogs.com/xiaohaigegede/p/13520225.html