代码:这个代码是有问题的,问题的产生是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. docker下安装 Oracle11gR2

    这是第二次安装,在第一次安装过程部分内容参考自如下: http://blog.sina.com.cn/s/blog_d840ff330102v4j0.html docker下oracle11g安装 h ...

  2. Node.Js and Mongoose

    Mongoose官方API,我做完之后整理出来的心得. ONE· Getting Started First be sure you have MongoDB and Node.js installe ...

  3. 【LeetCode】005. Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  4. Why getting this error “django.db.utils.OperationalError: (1050, ”Table 'someTable' already exists“)”

    0down votefavorite   I am getting error like django.db.utils.OperationalError: (1050, "Table 's ...

  5. vim直接编辑远程文件

    本文由Suzzz原创,发布于http://www.cnblogs.com/Suzzz/p/4116341.html,转载请保留此声明. Linux环境下 vim scp://user@hostIP/P ...

  6. ubuntu lts install licode tag pre-v5.4

    1. Requirements Ubuntu 14.04 LTS 2. Clone Licode codeYou first need to clone our code from github.Yo ...

  7. 一根Express Route同时支持ARM和ASM的VNET

    ARM模式的Azure管理模式在China Azure上已经正式落地了.今后在China Azure上应该主要以ARM的模式创建VM了. 并且目前Express Route也已经可以在ARM模式下创建 ...

  8. SpringMVC的环境搭建

    MyBatis框架-->持久层框架-->Object[对象]Relation[关系型数据库]Mapping[在MyBatis的体现是哪个映射文件中国的<resultMap>标签 ...

  9. JSF结合Spring 引入ViewScope

    当JSF项目的faceConfig中配置了Spring的配置代码 <application> <el-resolver>org.springframework.web.jsf. ...

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

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