代码:这个代码是有问题的,问题的产生是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. [SP16580]QTREE7

    luogu vjudge 题意 一棵树,每个点初始有个点权和颜色(输入会给你) 0 u :询问所有u,v路径上的最大点权,要满足u,v路径上所有点的颜色都相同 1 u :反转u的颜色 2 u w :把 ...

  2. centos7 showdoc 手动安装部署

    1.从 https://github.com/star7th/showdoc 下载压缩包: 2.解压文件,然后将showdoc-master下面的文件上传至服务器对应的文件下面: 3.在浏览器中输入 ...

  3. python之 centos6.7下 python 3.5.2 源码、Django-1.9 安装

    在linux6.5中已经自带了python 2 .python 2.6 ,并且yum程序使用的就是自带的python,所以系统自带的python不要随意卸载否则可能导致yum用不了. 测试环境:cen ...

  4. flask+blueprint路由配置

    1.flask默认的静态文件和html文件在app应用文件夹里的相应文件夹下:app // Flask||--static ||--templates |静态文件默认的url地址为:url_prefi ...

  5. 蓝桥杯 基础练习 BASIC-22 FJ的字符串

    基础练习 FJ的字符串   时间限制:1.0s   内存限制:512.0MB 问题描述 FJ在沙盘上写了这样一些字符串: A1 = “A” A2 = “ABA” A3 = “ABACABA” A4 = ...

  6. 机器学习:集成学习(Ada Boosting 和 Gradient Boosting)

    一.集成学习的思路 共 3 种思路: Bagging:独立的集成多个模型,每个模型有一定的差异,最终综合有差异的模型的结果,获得学习的最终的结果: Boosting(增强集成学习):集成多个模型,每个 ...

  7. c# pictureBox 循环播放图片

    c# 1.遍历目录 查找图片 2.在 pictureBox 循环播放 public void PlayThread()//CMD_UpdateBtnStatus cmd { Int32 framera ...

  8. 基于opencv+ffmpeg的镜头分割

    镜头分割常常被用于视频智能剪辑.视频关键帧提取等场景. 本文给出一种解决镜头分割问题的思路,可分为两个步骤: 1.根据镜头分割算法对视频进行分割标记 核心在于镜头分割算法,这里简单描述一种算法思路:r ...

  9. BA 新web化 问题汇总

    1. 3D堆栈图在winform端无法显示,但在web端可以正常显示,说明与浏览器版本有关,在 IE 中设置文档模式为 IE8 即报错,IE9 却正常显示,可在 <head>节点下添加如下 ...

  10. C#理解泛型(源代码)及 default(T)

    1.类型不安全.且代码无法遍历重用的源代码. 2.泛型源代码 源代码下载: http://files.cnblogs.com/files/qqhfeng/ConsoleApplication1.rar