LeetCode —— Merge k Sorted Lists
/*
** 算法的思路:
** 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的更多相关文章
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- LeetCode:Merge k Sorted Lists
题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- LeetCode——Merge k Sorted Lists
Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...
- leetcode -- Merge k Sorted Lists add code
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [ ...
- LeetCode Merge k Sorted Lists (链表)
题意 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- Leetcode:Merge k Sorted Lists分析和实现
题目大意是传入一个链表数组lists,每个链表都由若干个链接的链表结点组成,并且每个链表结点记录一个整数.题目保证传入的链表中的整数按从小到大进行排序. 题目要求我们输出一个新的链表,这个链表中应该包 ...
- LeetCode Merge k Sorted Lists 解决报告
https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...
随机推荐
- JavaScript写一个连连看的游戏
天天看到别人玩连连看, 表示没有认真玩过, 不就把两个一样的图片连接在一起么, 我自己写一个都可以呢. 使用Javascript写了一个, 托管到github, 在线DEMO地址查看:打开 最终的效果 ...
- Jquery.min.js 下载
Jquery 下载:http://www.jq22.com/jquery-info122
- linux centos中使用yum安装tomcat
在linux下部署java开发的web应用,一般采用Tomact+jre环境(可不需要apache),在RHEL和CentOS下,可以采用yum在线自动安装方式安装,具体操作如下: 可以先查看tomc ...
- selenium+eclispse里代码备注
1.火狐.谷歌和IE浏览器引擎都要重新下载selenium官网引擎,并设置路径才可以支持selenium3 而狐火用自己的引擎不用设置路径既可以支持selenium2也支持selenium3,谷歌和I ...
- 系统间通信(4)——IO通信模型和JAVA实践 中篇
4.多路复用IO模型 在"上篇"文章中,我们已经提到了使用多线程解决高并发场景的问题所在,这篇文章我们开始 4-1.现实场景 我们试想一下这样的现实场景: 一个餐厅同时有100位客 ...
- 【POJ 2406】Power Strings(KMP循环节)
终于靠着理解写出KMP了,两种KMP要代码中这种才能求循环节.i-next[i]就是循环节. #include<cstdio> #define N 1000005 char s[N]; i ...
- KM算法及其优化的学习笔记&&bzoj2539: [Ctsc2000]丘比特的烦恼
感谢 http://www.cnblogs.com/vongang/archive/2012/04/28/2475731.html 这篇blog里提供了3个链接……基本上很明白地把KM算法是啥讲清楚 ...
- ActiveMQ;RabbitMQ;ZeroMQ
中间件类型: Embedded middleware: As the name suggests, this typeof middleware handles embedded applicatio ...
- BIEE定制化
(1)自定义图片的引用 (2)修改产品本身的一些图片内容 (3)修改产品本身的一些文字 如何引用自己的自定义图片: 直接找路径或者图片就可以修改 推荐不要直接替换,直接替换导致有的内容没办法直接显示出 ...
- 1.Android常见异常:android.view.WindowLeaked 分析以及解决办法
在项目中遇到WindowManager: Activity has leaked window问题,其实在stackoverflow.com可以找到详细答案:http://stackoverflow ...