leetcode mergeKsortedlink
代码:这个代码是有问题的,问题的产生是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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- 西瓜书概念整理(chapter 1-2)熟悉机器学习术语
括号表示概念出现的其他页码, 如有兴趣协同整理,请到issue中认领章节 完整版见我的github:ahangchen 觉得还不错的话可以点个star ^_^ 第一章 绪论 Page2: 标记(lab ...
- OSError: [Errno 22] Invalid argument: 'D:\x07ckup\test.txt'
使用with open("D:\backup\test.txt","rt") as f:报上面的错误,将上面的语句改成 with open(r"D:\ ...
- UDP10040 和 setsockopt设置大全
今天无意之中碰到 UDP 10040 错误 原来是缓冲区不够,以下转载的解决方法以供不时之需. 1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该sock ...
- redis之 Redis常用数据类型
Redis最为常用的数据类型主要有以下7种: 一. String (字符) 常用命令: set,get,decr,incr,mget 等. 应用场景:String是最常用的一种数据类型,普通的key ...
- laravel config文件的使用
好多东西 由于许多地方都要使用与将来可能发生更改 我们需要把它提取出来 作为配置文件来使用 这样将来要修改的时候 只需要修改一处即可 学习源头: https://blog.csdn.net/linyu ...
- JAVA 正则表达式 Regex
//正则表达式,去掉空格.换行.制表位 public static void replaceBlank(){ Pattern p = Pattern.compile("\\s*|\t|\r| ...
- 第九章 Java中线程池
Java中的线程池是运用场景最多的并发框架,几乎所有需求异步或并发执行任务的程序都可以使用线程池.在开发过程中,合理地使用线程池能够带来3个好处. 降低资源消耗:通过重复利用已创建的线程降低线程创建和 ...
- Java中弱引用、软引用、虚引用及强引用的区别
Java中弱引用VS软引用 Java中有如下四种类型的引用: 强引用(Strong Reference) 弱引用(WeakReference) 软引用(SoftReference) 虚引用(Phant ...
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's parent first.
错误信息: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must ...
- 配gzip的过滤器进行压缩解决表单加载慢问题
一个客户的表单上字段超过五百,经浏览器的调试器发现主要问题是从服务器取数据花费了大量时间,下载内容大小约1.2M,下载时间在10s左右,导致样式加载完大约在17s左右(不清除浏览器缓存).最终考虑利用 ...