/*
** 算法的思路:
** 1.将k个链表的首元素进行建堆
** 2.从堆中取出最小的元素,放到链表中
** 3.如果取出元素的有后续的元素,则放入堆中,若没有则转步骤2,直到堆为空
*/ #include <stdio.h> struct ListNode
{
int val;
struct ListNode *next;
}; #define PARENT(i) (((i)-1)/2)
#define LEFT(i) ((i)*2+1)
#define RIGHT(i) ((i)*2+2) typedef struct ListNode * ListNodePointer; void MinHeapify(ListNodePointer lists[], int nListSize, int nParentIndex );
void BuildMinHeap(ListNodePointer lists[], int nListSize );
ListNodePointer ExtractMin( ListNodePointer lists[], int *pListSize );
void InsertHeap(ListNodePointer lists[], int *pListSize, ListNodePointer pNode );
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize); int main(int argc, char const *argv[])
{ struct ListNode list[] = {{.next=NULL,.val=}, {.next=NULL, .val=-}};
ListNodePointer pListPointerArray[] = {&list[], NULL, &list[]}; ListNodePointer header = mergeKLists(pListPointerArray, ); ListNodePointer pList = header;
while( NULL != pList )
{
printf("%d ", pList->val);
pList = pList->next;
}
printf("\n"); return ;
} void MinHeapify(ListNodePointer lists[], int nListSize, int nParentIndex )
{ int nLeftIndex; //左节点下标
int nRightIndex; //右节点下标
int nMinIndex; //最小节点下标
ListNodePointer pNodePtrTmp; do
{
nLeftIndex = LEFT(nParentIndex);
nRightIndex = RIGHT(nParentIndex);
nMinIndex = nParentIndex; if ( nLeftIndex < nListSize && lists[nLeftIndex]->val < lists[nMinIndex]->val )
{
nMinIndex = nLeftIndex;
} if ( nRightIndex < nListSize && lists[nRightIndex]->val < lists[nMinIndex]->val )
{
nMinIndex = nRightIndex;
} if ( nMinIndex != nParentIndex )
{
pNodePtrTmp = lists[nMinIndex];
lists[nMinIndex] = lists[nParentIndex];
lists[nParentIndex] = pNodePtrTmp; nParentIndex = nMinIndex; }else
{
break;
} }while( );
} //建堆
void BuildMinHeap(ListNodePointer lists[], int nListSize )
{ int i;
for ( i = nListSize/; i >= ; i-- )
{
MinHeapify(lists, nListSize, i);
}
} //从堆中取出最小的元素
ListNodePointer ExtractMin( ListNodePointer lists[], int *pListSize )
{ ListNodePointer pMinNode = lists[]; (*pListSize)--;
lists[] = lists[*pListSize]; MinHeapify(lists, *pListSize, ); return pMinNode;
} //向堆中添加元素
void InsertHeap(ListNodePointer lists[], int *pListSize, ListNodePointer pNode )
{ int nCurNodeIndex = *pListSize;
int nParentIndex = PARENT(nCurNodeIndex);
ListNodePointer pNodePtrTmp; lists[nCurNodeIndex] = pNode;
(*pListSize)++; while ( nCurNodeIndex > && lists[nParentIndex]->val > lists[nCurNodeIndex]->val )
{
pNodePtrTmp = lists[nParentIndex];
lists[nParentIndex] = lists[nCurNodeIndex];
lists[nCurNodeIndex] = pNodePtrTmp; nCurNodeIndex = nParentIndex;
nParentIndex = PARENT(nCurNodeIndex);
}
} struct ListNode* mergeKLists(struct ListNode** lists, int listsSize)
{ ListNodePointer *pListPointerArray = (ListNodePointer *) malloc( sizeof(ListNodePointer)*listsSize );
struct ListNode header = {.next=NULL};
ListNodePointer pTail = NULL; int i;
int nHeapSize = ; for( i=; i<listsSize; i++ )
{
if ( lists[i] != NULL )
{
pListPointerArray[nHeapSize] = lists[i];
nHeapSize++;
}
} if ( nHeapSize == )
{
return NULL;
} BuildMinHeap(pListPointerArray, nHeapSize); //这里为预处理
header.next = ExtractMin(pListPointerArray, &nHeapSize);
pTail = header.next;
if ( NULL != pTail && pTail->next != NULL )
{
InsertHeap(pListPointerArray, &nHeapSize, pTail->next );
} while( nHeapSize != )
{
pTail->next = ExtractMin(pListPointerArray, &nHeapSize); pTail = pTail->next; if ( NULL != pTail && NULL != pTail->next )
{
InsertHeap(pListPointerArray, &nHeapSize, pTail->next );
}
} free(pListPointerArray); return header.next;
}

LeetCode —— Merge k Sorted Lists的更多相关文章

  1. LeetCode: Merge k Sorted Lists 解题报告

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

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

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

  3. LeetCode:Merge k Sorted Lists

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

  4. LeetCode——Merge k Sorted Lists

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

  5. leetcode -- Merge k Sorted Lists add code

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

  6. LeetCode Merge k Sorted Lists (链表)

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

  7. [Leetcode] Merge k sorted lists 合并k个已排序的链表

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

  8. Leetcode:Merge k Sorted Lists分析和实现

    题目大意是传入一个链表数组lists,每个链表都由若干个链接的链表结点组成,并且每个链表结点记录一个整数.题目保证传入的链表中的整数按从小到大进行排序. 题目要求我们输出一个新的链表,这个链表中应该包 ...

  9. LeetCode Merge k Sorted Lists 解决报告

    https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...

随机推荐

  1. 【SPOJ 694】Distinct Substrings 不相同的子串的个数

    不会FQ啊,没法评测啊,先存一下代码QAQ 2016-06-16神犇Menci帮我测过AC了,谢谢神犇Menci QwQ #include<cstdio> #include<cstr ...

  2. mysql-数据类型与java数据类型转化工具类

    mysql和java对照表 类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述             VARCHAR L+N VARCHAR java.lang.Stri ...

  3. html-css控制背景图全屏拉伸不重复显示

    在HTML中,当我们设置背景图,只能采用是否重叠.居中.重叠方向这几个选项 CSS3中设置 body { background:#3d71b8 url(../back_main.png); backg ...

  4. 【USACO 2.2】Subset Sums (DP)

    N (1 <= N <= 39),问有多少种把1到N划分为两个集合的方法使得两个集合的和相等. 如果总和为奇数,那么就是0种划分方案.否则用dp做. dp[i][j]表示前 i 个数划分到 ...

  5. BZOJ 4325: NOIP2015 斗地主

    4325: NOIP2015 斗地主 Time Limit: 30 Sec  Memory Limit: 1024 MBSubmit: 684  Solved: 456[Submit][Status] ...

  6. 【BZOJ-4562】食物链 记忆化搜索(拓扑序 + DP)

    4562: [Haoi2016]食物链 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 133  Solved: 112[Submit][Status] ...

  7. 安装Mysql提示1045错误解决方法

    MySQL安装提示一下错误 The security settings could not be applied to the database because the connection has ...

  8. 洛谷P1565 牛宫

    题目描述 AP 神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M 的矩形空地. 空地中每个格子都有自己的海拔高度.AP 想让他的宫殿的平均海拔在海平面之上(假设 海平面的高度是0,平均数都会 ...

  9. QIBO CMS /inc/common.inc.php Local Variables Overriding Vul In $_FILES

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 齐博在/inc/common.inc.php使用$$_key=$value.ext ...

  10. ExceptionLess异常日志收集框架-1

    哈哈,中秋和代码更配哦,不知不觉一年过半了,祝园友们中秋快乐 前一阵子在博客园看到了一篇博文 http://www.cnblogs.com/savorboard/p/exceptionless.htm ...