/*
** 算法的思路:
** 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. Mybatis 自动生成代码

    准备条件: 将下面的文件放入同一目录下 操作步骤: 1/ 在 generatorConfig.xml 中配置相关的参数,与需要被自动生成的表 也可以 执行项目中的MybatisConfigAutoGe ...

  2. 如何才能恢复Excel文档的打开密码

    对于一些密码的破解,最常用的方法就是“暴力破解”,也是获取密码的最后一种方法,Advanced Office Password Recovery的暴力破解能够破解复杂的Office文档密码.wps也有 ...

  3. gene_abundance_estimation

    /home/liuhui/bin/trinityrnaseq_r20140413p1/util/support_scripts/get_Trinity_gene_to_trans_map.pl Tri ...

  4. asp.net mvc 外网获取不到port问题解决

    var IPandPort = Request.ServerVariables["HTTP_HOST"];

  5. Xcode 修改工程名以及注意事项

    1.先把整个工程文件夹名改为新的工程名. 2.打开工程,单击,输入新的工程名,会出现,点击确定. 3.回到工程界面,在中选择 Manage Schemes,然后再弹出的对话框,把工程名改为新的名字. ...

  6. AppVeyor-CI为GitHub项目做自动化集成(dotnet为主)

    travis-ci对dotnet的项目做自动化集成不太友好,尤其是使用mono的编译和不能使用MSTest进行自动化测试,所以转到appveyor进行. appveyor的配置非常简单,有两种方式: ...

  7. [iOS 视频流开发-获得视频帧处理]

    调用视频流所使用框架:<Foundation/Foundation.h> 必须定义的参数: 1.AVCaptureDevice(捕获设备:前置.后置摄像头等) 2.AVCaptureInp ...

  8. [IOS 实现TabBar在Push后的隐藏 以及 两级Tabbar的切换]

    翻了好多网页都没找到资料,自己试了下终于成功了,遂分享一下. 1.实现TabBar在Push后的隐藏 假如结构是这样 NavController->A->B,我们想要实现在A里有Tabba ...

  9. PHP扩展——C扩展实现滚动记录日志

    前言 万事开头难,没错就是这样!! 在没有真正开发PHP扩展之前,一直觉得PHP扩展开发对我来说是一个很遥远的事情,虽然自己有些C\C++基础,但是看PHP源码的时候还是很吃力,现在看来主要还是没有下 ...

  10. PHP中的一个”坑“

    说一个极有可能在工作中遇到的问题——foreach的引用 foreach $arr = range(1,3); //[1,2,3] foreach($arr as &$val) { } for ...