Merge k Sorted Lists
来源:互联网 发布:steam mac版怎么安装 编辑:程序博客网 时间:2024/05/29 02:56
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */bool operator <(const pair<ListNode*,int> &p1, const pair<ListNode*,int> &p2){ return p1.first->val < p2.first->val;}/*假设投针数组为副本,且由其它地方销毁*/#include<queue>class Solution {public: ListNode* mergeKLists(vector<ListNode*>& lists) { ListNode* helpNode = new ListNode(0); ListNode* cur = helpNode; typedef pair<ListNode*,int> heaptype; priority_queue<heaptype,vector<heaptype>,greater<heaptype>> q;//最小队列 for(int i = 0; i< lists.size(); ++i){ if(lists[i]){ q.push(make_pair(lists[i],i)); lists[i] = lists[i]->next; } } while(!q.empty()){ ListNode* node = q.top().first; int index = q.top().second; q.pop(); if(lists[index]){ q.push(make_pair(lists[index],index)); lists[index] = lists[index]->next; } cur->next = new ListNode(node->val); cur= cur->next; } auto ret = helpNode->next; delete helpNode; return ret; }};
0 0
- Merge K Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge K Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge K sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- Merge k Sorted Lists
- wordpress极简搭建
- Leetcode: Number of 1 Bits
- CollectionView中的一个cell中加载xib中的CollectionView 不显示cell
- JPA常用注解
- Android 控件总结
- Merge k Sorted Lists
- springmvc + jquery datatable + ajax实现服务端动态分页查询
- c/c++对数组取地址
- uva 140 Bandwidth(全排列+递归)
- android CTS测试
- 静态网页
- Python pygraphviz 安装方法
- 编译型语言/解释型语言
- C标签定义变量,做加减乘除操作