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 ...
随机推荐
- SQL Server 学习系列之五
SQL Server 学习系列之五 SQL Server 学习系列之一(薪酬方案+基础) SQL Server 学习系列之二(日期格式问题) SQL Server 学习系列之三(SQL 关键字) SQ ...
- 前用户sudo免密码
Ubuntu 设置当前用户sudo免密码 方法1 # 备份 /etc/sudoers sudo cp /etc/sudoers . #打开 /etc/sudoers sudo visudo # 在 ...
- Asp.NET Core+ABP框架+IdentityServer4+MySQL+Ext JS之验证码
验证码这东西,有人喜欢有人不喜欢.对于WebApi是否需要验证码,没去研究过,只是原来的SimpleCMS有,就加上吧. 在WeiApi上使用验证码,关键的地方在于WeiApi是没有状态的,也就是说, ...
- 蓝桥杯 算法训练 ALGO-121 猴子分苹果
算法训练 猴子分苹果 时间限制:1.0s 内存限制:256.0MB 问题描述 秋天到了,n只猴子采摘了一大堆苹果放到山洞里,约定第二天平分.这些猴子很崇拜猴王孙悟空,所以都想给他留一些苹果 ...
- linux 将大文件分解为多个小文件
使用的命令为: split --bytes 500M --numeric-suffixes --suffix-length=3 foo foo_ 其中 --bytes 为小文件的大小, --suffi ...
- SQLite连接C#笔记
不得不吐槽,实在是太坑了.以下几点一定要注意: 要下载两个东西,都要上官网.一个是SQLite for Windows,一个是System.Data.SQLite. 下载下来的DLL里面有个test, ...
- boost的asio接收单路大数据量udp包的方法
开发windows客户端接收RTP视频流,当h264视频达到1080P 60fps的时候,按包来调用recvfrom的函数压力比较大,存在丢包的问题,windows的完成端口的性能效果当然可以解决这个 ...
- [转] LINUX 三种网络连接模式
Linux下NAT模式和桥接模式的网络配置 最近在配置linux虚拟机的时候发现有很多坑,现在记录下来以防日后又跳到坑里. 我的运行环境是:主机 windows 7 虚拟机 Virtualbox ...
- java数组复制的几种常见用法
1.1数组复制的几种常见用法 1.1.1System.arraycopy的用法 int[] src = {1,3,5,7,9,11,13,15,17}; int[] dest = {2,4,6,8,1 ...
- c#在sql中存取图片image示例
这篇文章主要介绍了c#在sql中存取图片image示例,需要的朋友可以参考下 (1)控制台应用程序下演示插入图片 复制代码 代码如下: public void InsertIMG() { //将需要存 ...