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已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...
随机推荐
- poj1236 强连通缩点
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15211 Accepted: 60 ...
- WordPress 博客文章时间格式the_time()设置
国外设计的WordPress 主题里的文章的时间格式是类似“十一月 21, 2010”这种格式的,而中国人习惯的是年在前,月紧跟其后,日在末尾,所以看国外的就显得很别扭,但是我们可以通过修改WP时间代 ...
- ThreadLocal详解(实现多线程同步访问变量)
ThreadLocal翻译成中文比较准确的叫法应该是:线程局部变量. 这个玩意有什么用处,或者说为什么要有这么一个东东?先解释一下,在并发编程的时候,成员变量如果不做任何处理其实是线程不安全的,各个线 ...
- 虚拟机NAT模式无法上网问题的解决办法
在使用CentOS虚拟机时,出现了无法上网的情况,使用主机ping虚机地址可以ping通,而虚机ping不通主机,同时虚机也无法ping通其他的网址或ip,显示内容为Network is unreac ...
- 新版汉诺塔(UVa10795 - A Different Task)
题目介绍: 标准的汉诺塔上有n个大小各异的盘子.现给定一个初始局面(见图1),求它到目标局面(见图2)至少需要移动多少步? 移动规则:一次只能移动一个盘子:且在移动盘子之前,必须把压在上面的其他盘子先 ...
- matlab之meshgrid()函数
以最常见的一个用法为例: [X,Y]=meshgrid(xgv, ygv) xgv是一个(一维的,行)向量,ygv也是. 产生的X和Y,规格相同,都是二维向量,高度为size(ygv,2),宽度为si ...
- 【BZOJ-4456】旅行者 分治 + 最短路
4456: [Zjoi2016]旅行者 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 254 Solved: 162[Submit][Status] ...
- CentOS彻底卸载wine
说明:折腾啊折腾,装了卸,卸了装,哈哈. 1. 卸载wine及相关组件 yum remove wine* 2. 删除相关文件 ~/.wine ~/.local/share/applications / ...
- Hong Kong Regional Online Preliminary 2016 C. Classrooms
Classrooms 传送门 The new semester is about to begin, and finding classrooms for orientation activities ...
- pycharm安装python模块
这个工具真的好好,真的很喜欢,它很方便,很漂亮,各种好 pycharm安装python模块:file-setting-搜索project inte OK