https://oj.leetcode.com/problems/merge-k-sorted-lists/

这道题主要是考虑测试数据的特点吧。

刚开始的时候想,每次找出头结点中最小的两个,然后取最小的一个,一直取到它的值 > 倒数第二小的,之后重复这个过程。

适合的数据特点为: 1 2 3 5 6 7 10 20

11 12 15 18 …… 这样子的

于是写出了代码,但是超时了。

超时的数据是这样的:[{7},{49},{73},{58},{30},{72},{44},{78},{23},{9},{40},{65},{92},{42},{87},{3},{27},{29},{40},……

也就是每个链表长度为1,这样的话,就多了好多去两个最小值的操作。

于是参考了答案,方法是每次合并两个链表,得到一个新链表,之后新链表再和下一个合并……

代码分别如下:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
ListNode *dummy = new ListNode();
if(lists.size() == )
return dummy->next; int count = ; // record null number
int small = ;
int bigger = ;
int smallestIndex = ;
int biggerIndex = ;
ListNode *currentNode = dummy; for(int i = ; i < lists.size(); i++)
{
if(lists[i] == NULL)
count++;
} while(count < lists.size() - )
{
findTwoLittle(lists,smallestIndex,biggerIndex);
currentNode->next = lists[smallestIndex]; while(lists[smallestIndex]->next != NULL &&lists[smallestIndex]->next->val <= lists[biggerIndex]->val)
{
lists[smallestIndex] = lists[smallestIndex]->next;
}
currentNode = lists[smallestIndex];
lists[smallestIndex] = lists[smallestIndex]->next;
if(lists[smallestIndex] == NULL)
count++;
}
// only one isn't null
for(int i = ; i < lists.size(); i++)
{
if(lists[i] != NULL)
{
currentNode->next = lists[i];
break;
}
}
return dummy->next;
}
//至少还有两个元素的时候
void findTwoLittle(vector<ListNode *> &lists, int &smallestIndex, int &biggerIndex)
{
int small = INT16_MAX;
int bigger = INT16_MAX;
smallestIndex = ;
biggerIndex = ; for(int i = ; i < lists.size(); i++)
{
if(lists[i] != NULL)
{
if(lists[i]->val <= small)
{
bigger = small;
small = lists[i]->val;
biggerIndex = smallestIndex;
smallestIndex = i;
}
else if(lists[i]->val > bigger)
continue;
else
{
bigger = lists[i]->val;
biggerIndex = i;
}
} }
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
if(lists.size() == )
return NULL;
ListNode *p = lists[];
for(int i = ; i < lists.size(); i++)
p = mergeTwoLists(p,lists[i]);
return p;
}
ListNode *mergeTwoLists(ListNode *l1,ListNode *l2){
ListNode head(-);
for(ListNode *p = &head; l1 != NULL || l2 != NULL; p = p->next)
{
int val1 = l1 == NULL? INT_MAX:l1->val;
int val2 = l2 == NULL? INT_MAX:l2->val;
if(val1 <= val2)
{
p->next = l1;
l1 = l1->next;
}
else
{
p->next = l2;
l2 = l2->next;
}
}
return head.next;
}
};

LeetCode OJ-- Merge k Sorted Lists *@的更多相关文章

  1. Leetcode OJ : Merge k Sorted Lists 归并排序+最小堆 mergesort heap C++ solution

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  2. Java for LeetCode 023 Merge k Sorted Lists

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

  3. 蜗牛慢慢爬 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 ...

  4. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  5. LeetCode 023 Merge k Sorted Lists

    题目要求:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and ...

  6. [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 ...

  7. [leetcode 23]Merge k Sorted Lists

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

  8. [LeetCode] 23. Merge k Sorted Lists ☆☆☆☆☆

    转载:https://leetcode.windliang.cc/leetCode-23-Merge-k-Sorted-Lists.html 描述 Merge k sorted linked list ...

  9. LeetCode 23 Merge k Sorted Lists(合并k个有序链表)

    题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...

  10. leetcode 【 Merge k Sorted Lists 】python 实现

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

随机推荐

  1. python3.7 装饰器

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 装饰器 #装饰器 ''' 定义:本质就是一个函数,作用是为其他函 ...

  2. 2940: [Poi2000]条纹(Multi_SG)

    2940: [Poi2000]条纹 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 114  Solved: 72[Submit][Status][Dis ...

  3. thinkpad alert键一直处于按着的状态

    就是alert 一直默认按着的,具体原因,我还没有见过. 但是解决方法很简单,crlt+alert一块按,就好了.

  4. MySQL基础6-分组查询

    1.分组函数 需求20:查询所有商品平均零售价SELECT AVG(salePrice) FROM product 需求21:查询商品总记录数SELECT COUNT(id) count FROM p ...

  5. loj2035 「SDOI2016」征途

    学了斜率优化这题就能一气呵成地做出来啦qwqqwq #include <iostream> #include <cstdio> using namespace std; typ ...

  6. laravel5.2总结--软删除

    当模型被软删除时,它们并不会真的从数据库中被移除.而是会在模型上设置一个 deleted_at 属性并将其添加到数据库.如果对应模型被软删除,则deleted_at字段的值为删除时间,否则该值为空. ...

  7. Python-S9-Day123——爬虫两示例

    01 今日内容回顾 02 内容回顾和补充:面向对象约束 03 爬虫之抽屉新热榜 04 爬虫之抽屉自动登录(一) 05 爬虫之抽屉自动登录(二) 06 爬虫之登录github(一) 07 爬虫之登录gi ...

  8. PHP 获取客户端用户 IP 地址

    一般情况下可以使用以下代码获取到用户 IP 地址 echo 'User IP - '.$_SERVER['REMOTE_ADDR']; // 服务器在局域网的话,那么显示的则是内网IP .// 如果服 ...

  9. 为什么对多线程编程这么怕?pthread,sem,mutex,process

    转自http://blog.chinaunix.net/uid-20788636-id-1841334.html 1.线程创建和退出创建线程实际上就是确定调用该线程函数的入口点,这里通常使用的函数是p ...

  10. BZOJ2555 SubString 【后缀自动机 + LCT】

    题目 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. 输入 ...