leetcode 合併數組

  1. 合併兩個有序鏈表
    將兩個升序鏈表合併爲一個新的 升序 鏈表並返回。新鏈表是通過拼接給定的兩個鏈表的所有節點組成的。
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
    {
        if(!l1 || !l2)
        {
            return l1 ? l1 : l2;
        }

        ListNode head(0);
        ListNode* tail = &head;
        ListNode* a = l1;
        ListNode* b = l2;

        while(a&&b)
        {
            if(a->val > b->val)
            {
                tail->next = b;
                tail = tail->next;
                b = b->next;
            }
            else
            {
                tail->next = a;
                tail = tail->next;
                a = a->next;
            }
        }

        tail->next = a ? a : b;

        return head.next;
        
    }
  1. 合併K個排序鏈表
    合併 k 個排序鏈表,返回合併後的排序鏈表。請分析和描述算法的複雜度。

示例:

輸入:
[
  1->4->5,
  1->3->4,
  2->6
]
輸出: 1->1->2->3->4->4->5->6

方法一:分治合併

ListNode* mergeKLists(vector<ListNode*>& lists) {
        //分治合併  遞歸
        ListNode* head = merge(lists, 0, lists.size()-1);

        return head;


    }
    ListNode* merge(vector<ListNode*>&lists, int l, int r)
    {
        if(l == r)
        {
            return lists[l];
        }
        if(l > r)
        {
            return nullptr;
        }

        int mid = (l+r)>>1;

        return mergetwo(merge(lists, l, mid), merge(lists, mid+1, r));

    }

    ListNode* mergetwo(ListNode* l, ListNode* r)
    {
        if(!l || !r)
        {
            return l ? l : r;
        }

        ListNode head(0);
        ListNode* tail = &head;
        ListNode* a = l;
        ListNode* b = r;

        while(a&&b)
        {
            if(a->val > b->val)
            {
                tail->next = b;
                tail = tail->next;
                b = b->next;
            }
            else
            {
                tail->next = a;
                tail = tail->next;
                a = a->next;
            }
        }

        tail->next = a ? a : b;

        return head.next;
        
    }

方法二:使用優先對列,每層的最小值,中找最小的取出。

//     struct VecNode{
//         int val;
//         ListNode * p;
//         bool operator < (const VecNode& rhs) const {   
//             return val > rhs.val;             //小根堆
//         }
//     };
//     ListNode* mergeKLists(vector<ListNode*>& lists) {
//         //使用優先隊列來合併
//         priority_queue<VecNode> que;
//         for(int i = 0; i < lists.size(); ++i)
//         {
//             if(lists[i])
//             que.push({lists[i]->val, lists[i]});
//         }

//         ListNode head(0);
//         ListNode * tail = &head;

//         while(!que.empty())
//         {
//             // cout << "s"<<endl;
//             auto p = que.top();
//             que.pop();
//             tail->next = p.p;
//             tail = tail->next;
//             if(p.p->next)
//             {
//                 que.push({p.p->next->val, p.p->next});
//             }

//         }

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