您的位置:首页 > 其它

lintcode merge-k-sorted-lists 合并k个排序链表

2016-07-24 15:22 288 查看

问题描述

lintcode

笔记

用到了堆/优先队列

代码1用到了STL的priority_queue

代码2用到了STL的heap,要特别注意
push_heap,pop_heap
的实际操作,我就被坑了一上午的时间。比如说,如果是小顶堆的话,
pop_heap
是把要删除的元素(最小的元素)放到了最后,然后再用
vec.pop_back()
才能正确地删除。还有cmp要写成
cmp()


代码3自己写了个heap

代码1 priority_queue

/**
* Definition of ListNode
* class ListNode {
* public:
*     int val;
*     ListNode *next;
*     ListNode(int val) {
*         this->val = val;
*         this->next = NULL;
*     }
* }
*/
class Solution {
public:
/**
* @param lists: a list of ListNode
* @return: The head of one sorted list.
*/
struct cmp
{
bool operator() (ListNode* a, ListNode* b)
{
return ((a->val) > (b->val));
}
};

ListNode *mergeKLists(vector<ListNode *> &lists) {
// write your code here
priority_queue<ListNode*, vector<ListNode*>, cmp> q;
ListNode* dummy = new ListNode(0);
ListNode* iter = dummy;
for (int i = 0; i < lists.size(); i++)
{
ListNode* nowNode = lists[i];
if (nowNode)
{
q.push(nowNode);
}
}
while (!q.empty())
{
ListNode* nowNode = q.top();
q.pop();
iter->next = nowNode;
iter = iter->next;
if (nowNode->next)
q.push(nowNode->next);
}
iter->next = NULL;
return dummy->next;
}
};


代码2 STL heap

/**
* Definition of ListNode
* class ListNode {
* public:
*     int val;
*     ListNode *next;
*     ListNode(int val) {
*         this->val = val;
*         this->next = NULL;
*     }
* }
*/
class Solution {
public:
/**
* @param lists: a list of ListNode
* @return: The head of one sorted list.
*/

struct cmp
{
bool operator () (ListNode* a, ListNode* b)
{
return a->val > b->val;
}
};

ListNode *mergeKLists(vector<ListNode *> &lists) {
// write your code here
vector<ListNode*> mylist;
for (int i = 0; i < lists.size(); i++)
{
if (lists[i])
mylist.push_back(lists[i]);
}
ListNode* dummy = new ListNode(0);
ListNode* iter = dummy;
make_heap(mylist.begin(), mylist.end(), cmp());
while (!mylist.empty())
{
pop_heap(mylist.begin(), mylist.end(), cmp());
ListNode* node = mylist.back();
iter->next = node;
iter = iter->next;
mylist.pop_back();
if (node->next)
{
mylist.push_back(node->next);
push_heap(mylist.begin(), mylist.end(), cmp());
}
}
iter->next = NULL;
return dummy->next;
}
};


代码3 自制heap

/**
* Definition of ListNode
* class ListNode {
* public:
*     int val;
*     ListNode *next;
*     ListNode(int val) {
*         this->val = val;
*         this->next = NULL;
*     }
* }
*/

void vecp(vector<ListNode*> v)
{
for (int i = 0; i < v.size(); i++)
cout << v[i]->val << ' ';
cout << endl;
}

class Solution {
public:
/**
* @param lists: a list of ListNode
* @return: The head of one sorted list.
*/
ListNode *mergeKLists(vector<ListNode *> &lists) {
// write your code here
ListNode* dummy = new ListNode(0);
ListNode* iter = dummy;
vector<ListNode*> mylist;
for (int i = 0; i < lists.size(); i++)
{
if (lists[i])
mylist.push_back(lists[i]);
}
makeMinHeap(mylist, mylist.size()-1);
// vecp(mylist);
while (!mylist.empty())
{
swap(mylist[0], mylist[mylist.size()-1]);
ListNode* node = mylist.back();
iter->next = node;
iter = iter->next;
mylist.pop_back();
minHeapFixDown(mylist, 0, mylist.size()-1);
if (node->next)
{
mylist.push_back(node->next);
minHeapFixUp(mylist, mylist.size()-1);
}
}
iter->next = NULL;
return dummy->next;
}

void minHeapFixDown(vector<ListNode*> &vec, int start, int end)
{
int par = start;
int minChild = par * 2 + 1;
while (minChild <= end)
{
if (minChild+1 <= end &
4000
amp;& vec[minChild]->val > vec[minChild+1]->val)
minChild++;
if (vec[minChild]->val >= vec[par]->val)
return;
// vecp(vec);
swap(vec[minChild], vec[par]);
// cout << "asd\n"
// vecp(vec);
par = minChild;
minChild = par * 2 + 1;
}
}

void makeMinHeap(vector<ListNode*> &vec, int end)
{
for (int i = (end - 1) / 2; i >= 0; i--)
minHeapFixDown(vec, i, end);
}

void minHeapFixUp(vector<ListNode*> &vec, int end)
{
int child = end;
int par = (end - 1) / 2;
while (par >= 0)
{
if (vec[par]->val <= vec[child]->val)// 应该是<=而不是<,否则会死循环
return;
swap(vec[par], vec[child]);
child = par;
par = (child - 1) / 2;
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lintcode heap