代码:这个代码是有问题的,问题的产生是map中不能存放相同的值。

 #include<iostream>
#include<vector>
#include<cmath>
#include<map> using namespace std; typedef struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}ListNode; inline int left(int x)
{
return * x + ;
} inline int right(int x)
{
return * x + ;
} void swap(int &x, int &y)
{
int c = x;
x = y;
y = c;
} int QQ; // 以i节点为根节点进行,堆的维护;假设i节点下面的节点都是堆了。
void MaxHeap(int a[], int i, int k,int ok)
{
int l = left(i);
int r = right(i);
int smallest = i;
if (l < k && a[l] < a[i])
{
smallest = l;
}
if (r<k&&a[r] < a[smallest])
{
smallest = r;
}
if (i == smallest)
{
return;
}
else
{
swap(a[smallest], a[i]);
return MaxHeap(a, smallest, k, );
}
} ListNode *mergeKLists(vector<ListNode *> &lists) {
int k = lists.size();
int *flag = new int[k]; // 标志位
int *a = new int[k];
ListNode *head;
map<int, int> cmap;
int n = k;
ListNode **p = (ListNode**)malloc(sizeof(ListNode*)*k);
for (int i = ; i < k; i++)
{
p[i] = lists[i];
a[i] = p[i]->val;
cmap.insert(make_pair(a[i], i));
if (p[i] == NULL)
{
flag[i] = ;
}
else
{
flag[i] = ;
}
}
MaxHeap(a, , k,);
int t = cmap[a[]];
head = lists[t];
ListNode *m = lists[t];
p[t] = p[t]->next;
while (n != )
{
n = k;
for (int i = ; i < k; i++)
{
if (p[i] == NULL)
{
flag[i] = ;
n--;
}
}
if (flag[t] == )
{
a[] = a[n];
MaxHeap(a, , n, );
t = cmap[a[]];
m->next = p[t];
m = m->next;
p[t] = p[t]->next;
}
else
{
a[] = p[t]->val;
cmap.insert(make_pair(a[], t));
MaxHeap(a, , n, );
t = cmap[a[]];
m->next = p[t];
m = m->next;
p[t] = p[t]->next;
}
}
ListNode * last;
for (int i = ; i < k; i++)
{
if (p[i] != NULL)
{
last = p[i];
}
}
m->next = last;
return head;
} int main()
{
/*int a[] = {3,1,4,2,3,5,6};
MaxHeap(a, 0, 7, 0);
cout << "标识"<<QQ << endl;
for (int i = 0; i < 7; i++)
{
cout << a[i] << endl;
}*/
int a[] = {,,,, , };
int b[] = {,, , , ,,};
int c[] = { , , , };
vector<ListNode*> p; ListNode *temp = (ListNode *)malloc(sizeof(ListNode));
ListNode *head = temp;
for (int i = ; i < ; i++)
{
temp->val = a[i];
if (i != )
temp->next = (ListNode *)malloc(sizeof(ListNode));
else
temp->next = NULL;
temp = temp->next;
}
ListNode *temp1 = (ListNode *)malloc(sizeof(ListNode));
ListNode *head1 = temp1;
for (int i = ; i < ; i++)
{
temp1->val = b[i];
if (i != )
temp1->next = (ListNode *)malloc(sizeof(ListNode));
else
temp1->next = NULL;
temp1 = temp1->next;
} ListNode *temp2 = (ListNode *)malloc(sizeof(ListNode));
ListNode *head2 = temp2;
for (int i = ; i < ; i++)
{
temp2->val = c[i];
if (i != )
temp2->next = (ListNode *)malloc(sizeof(ListNode));
else
temp2->next = NULL;
temp2 = temp2->next;
} p.push_back(head);
p.push_back(head1);
p.push_back(head2);
for (ListNode * temp = head; temp != NULL; temp = temp->next)
{
cout << temp->val << endl;
}
for (ListNode * temp = mergeKLists(p); temp != NULL; temp = temp->next)
{
cout << temp->val << endl;
}
}

leetcode mergeKsortedlink的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. shell 去重

    group=`cat config.properties  |            grep -v "^$" |                 grep -v "^# ...

  2. VisualPage重定向

    开发文档链接:https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_override ...

  3. webrtc 术语

    参考网址 https://webrtcglossary.com/ ORTC stands for Object-RTC.ORTC is an initiative involving Google, ...

  4. jQuery.extend()方法

    定义和用法 jQuery.extend()函数用于将一个或多个对象的内容合并到目标对象. 注意: 1. 如果只为$.extend()指定了一个参数,则意味着参数target被省略.此时,target就 ...

  5. SOAP webserivce 和 RESTful webservice 对比及区别

    简单对象访问协议(Simple Object Access Protocol,SOAP)是一种基于 XML 的协议,可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),简单邮件传 ...

  6. java中i/o练习

    总结: FileInputStream fis; int length; while((length=fis.read(b,0,b.length))!=-1){ output.write(b,0,le ...

  7. String字符串补0操作常见方法

     String前补0 java的String字符串补0或空格 方法一:自己写的方法 /* *数字不足位数左补0** @param str* @param strLength*/public stati ...

  8. calc PI

    https://en.wikipedia.org/wiki/Pi code https://github.com/HHS-IntroProgramming/Calculate-Pi https://g ...

  9. 关于pipe管道的读写端关闭问题

    知识概述 通过pipe在内核中创建一个文件,然后可以实现两个进程通信 管道是一种最基本的IPC机制,由 pipe 函数创建: #include <unistd.h> ]); 调用 pipe ...

  10. [更新中]【South使用总结】django开发中使用South进行数据库迁移

    Django开发中使用South进行数据库迁移的使用总结 South的详细资料可产看官方文档http://south.readthedocs.org/en/latest South安装配置 pip i ...