一、题目说明

这个题目是23. Merge k Sorted Lists,归并k个有序列表生成一个列表。难度为Hard,实际上并不难,我一次提交就对了。

二、我的解答

就是k路归并,思路很简单,实现也不难。

#include<iostream>
#include<vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists){
ListNode dummy(-1);
ListNode *p,*pResult,*cur;
if(lists.size()<=0) return NULL;
for(int t=0;t<lists.size();t++){
p = lists[t];
pResult = &dummy;
while(p !=NULL){
while(pResult->next!=NULL && pResult->next->val < p->val){
pResult = pResult->next;
}
cur = new ListNode(p->val);
cur->next = pResult->next;
pResult->next = cur;
p = p->next;
}
}
return dummy.next;
}
};
int main(){
Solution s;
ListNode* l1, *l2, *l3, *p; // init l1
l1 = new ListNode(5);
p = new ListNode(4);
p->next = l1;
l1 = p;
p = new ListNode(1);
p->next = l1;
l1 = p; // init l2
l2 = new ListNode(4);
p = new ListNode(3);
p->next = l2;
l2 = p;
p = new ListNode(1);
p->next = l2;
l2 = p; // init l1
l3 = new ListNode(6);
p = new ListNode(2);
p->next = l3;
l3 = p; vector<ListNode*> lists;
lists.push_back(l1);
lists.push_back(l2);
lists.push_back(l3); ListNode* r = s.mergeKLists(lists);
while(r != NULL){
cout<<r->val<<" ";
r = r->next;
}
return 0;
}

不过,性能一般:

Runtime: 172 ms, faster than 21.46% of C++ online submissions for Merge k Sorted Lists.
Memory Usage: 12.8 MB, less than 5.95% of C++ online submissions for Merge k Sorted Lists.

三、优化措施

上面的实现,之所以性能不足,在于一次归并一个队列,用的是插入排序。其实n路归并,可以用优先级队列priority_queue一次实现的。

class Solution {
struct CompareNode {
bool operator()(ListNode* const & p1, ListNode* const & p2) {
// return "true" if "p1" is ordered before "p2", for example:
return p1->val > p2->val;
//Why not p1->val <p2->val; ??
}
};
public:
ListNode *mergeKLists(vector<ListNode *> &lists) { ListNode dummy(0);
ListNode* tail=&dummy; priority_queue<ListNode*,vector<ListNode*>,CompareNode> queue; for (vector<ListNode *>::iterator it = lists.begin(); it != lists.end(); ++it){
if (*it)
queue.push(*it);
}
while (!queue.empty()){
tail->next=queue.top();
queue.pop();
tail=tail->next; if (tail->next){
queue.push(tail->next);
}
} return dummy.next;
}
};

刷题23. Merge k Sorted Lists的更多相关文章

  1. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  2. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  3. 23. Merge k Sorted Lists - LeetCode

    Question 23. Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 ...

  4. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  5. [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  6. leetcode 23. Merge k Sorted Lists(堆||分治法)

    Merge k sorted linked lists and return it as one sorted list. 题意:把k个已经排好序的链表整合到一个链表中,并且这个链表是排了序的. 题解 ...

  7. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

  8. [leetcode 23]Merge k Sorted Lists

    1 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...

  9. 23. Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. = ...

随机推荐

  1. 学习Java的书籍资料

    对于程序员来说,编程技术至关重要,然而技术的提高不是一蹴而就的,它需要时间的积累和经验的沉淀.因此本文为大家推荐Java学习的书籍,学虽容易,学好不易,且学且珍惜. 基础类.<Java从入门到精 ...

  2. Centos7 更新配置为阿里源步骤

    一.yum更换配置源过程 1.备份原有的.repo源文件 首先需要将之前的源进行备份(一般重要的配置文件都需要有备份的意识) # 进入源配置目录 cd /etc/yum.repos.d # 创建备份文 ...

  3. Design and History FAQ for Python3

    Source : Design and History FAQ for Python3 Why is there no goto? 你可以通过异常来获得一个可以跨函数调用的 "goto 结构 ...

  4. Lucene_solr

    1.总结 https://pan.baidu.com/s/1pMAWk0z  密码:ekhx 2.代码 https://pan.baidu.com/s/1nxmTWy1   密码:65ec 3.资料 ...

  5. ZB本地设置

    (1)web.config <!--本地服务器--> <add name="connectionString" connectionString="mG ...

  6. H5地理定位获取用户当前位置、城市

    第一步:需要在百度地图开发者平台创建一个应用:http://lbsyun.baidu.com/apiconsole/key/create 配置信息 申请配置成功以后返回一个AK 第二步:引入百度地图的 ...

  7. oracle jobs查看 sql及创建 jobs

    1.查看所有的 jobs select t.* from user_jobs t 2.创建 jobs declare job_id   pls_integer; begin sys.dbms_job. ...

  8. 【深入】 - AST抽象语法树

    参考: https://segmentfault.com/a/1190000016231512

  9. Random Process Modeling_1

    1. Bertrand Triangle r=1; %circle radius x0=0; y0=0; %centre of circle %points for circle t=linspace ...

  10. 2.Jsoup

    public static void main(String[] args) { //爬取最大资源网上的数据 //用CSS选择器 try { Document doc = Jsoup.parse(ne ...