/*
** 算法的思路:
** 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. Swift基础--Swift中的分类以及在分类中扩展init方法的注意事项

    Swift中的分类 1.创建一个空的swift文件 2.关键字extension,格式: extension 要扩展的类名 {} extension UIButton { } Swift中扩展init ...

  2. PyQt类库介绍

    安装完PyQt后我们去看看这个库里面有些什么cd /usr/lib/python3/dist-packages/PyQt5/  && ls都是些.so的动态链接库,这就是为什么我们在安 ...

  3. Java栈的实例模拟

    前言: “后进先出”---是栈(Stack)这种数据结构最基本的特点.很多程序设计语言都具有封装好的Stack工具,本文就带领大家一起将栈温习一下并附上一个模拟栈的程序. Java内存分配中,每通过n ...

  4. (转)CSS的Sprites技术

    Css Sprites 技术逐渐流行,各大网站上都可以看到它的身影. 但从本质上,Css Sprites 只是 Css 技术的一个使用小窍门,初学者也能快速上手. Css Sprites 简单解释: ...

  5. 【BZOJ-2618】凸多边形 计算几何 + 半平面交 + 增量法 + 三角剖分

    2618: [Cqoi2006]凸多边形 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 959  Solved: 489[Submit][Status] ...

  6. 【BZOJ-1976】能量魔方Cube 最小割 + 黑白染色

    1976: [BeiJing2010组队]能量魔方 Cube Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 884  Solved: 307[Submi ...

  7. 上传文件到hdfs注意事项

    我在MapReduceInput下创建CFItemSet文件夹,下面有itemSet.txt. 我想上传到cf下,然后想着hdfs上会显示cf/itemSet.txt. hdfs dfs -put i ...

  8. 【bzoj1588】 HNOI2002—营业额统计

    http://www.lydsy.com/JudgeOnline/problem.php?id=1588 (题目链接) 题意 给出一个序列,对于每一个数,找出之前与它相差最小的数,两者相减取绝对值加入 ...

  9. eclipse安装插件的方法(简单、ERMaster插件安装)

    ERMaster插件:https://sourceforge.net/projects/ermaster/ 链接:http://pan.baidu.com/s/1o7UWLMa 密码:wkax 可以编 ...

  10. 负载均衡下的资源文件配置/多站点下的资源文件夹共享(Windows IIS)

    前言: 负载均衡用的是NLB,微软的方案不太靠谱,举个例子吧,AB两台服务器负载出C,如果用户访问访问C之后分配的是A,那么如果A挂了,是不会自动切换到B的.据说后来还有一种NLB的方案可以实现,也不 ...