-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23. Merge k Sorted Lists
130 lines (108 loc) · 4.04 KB
/
23. Merge k Sorted Lists
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-----------------------------------------------------------question------------------------------------------------------
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6
------------------------------------------------------------analyse-----------------------------------------------------
这道题侧重数据结构的运用, 很多时候数据结构看起来比较简单。但是运用的时候却会束手无策。该题目用的结构相当于是一个
单链表的结构,但是将子结构放到一个向量中嵌套,需要灵或调用该结构并比较大小。
这道题我用两个方法来讲分别时间复杂度较高但空间复杂度较低的方法(简单容易理解),以及降低时间复杂度增加空间复杂度
的方法。
------------------------------------------------------------solution----------------------------------------------------
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
////////////以上为链表的结构
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode *head = new ListNode(0), *cur = head;
int min = INT_MAX, idx = 0;
while(true){
for (int i=0; i<lists.size(); i++){
if(lists[i] && lists[i]->val < min){
min = lists[i] -> val;
idx = i; //首先找到头部数值最小的子序列,在向量的第几位
}
}
if(min == INT_MAX){ // 如果序列为空或满足条件则跳出 min == INI_MAX
break;
}
cur->next = lists[idx];
cur = cur->next;//逐渐相当于新建的ListNode后一位便于添加新的当前子序列的第一个值中的最小值
lists[idx] = lists[idx]->next;//被调用的子序列的头指向下一个元素便于比较
min = INT_MAX; //设置条件要是比较完成则满足条件退出循环
}
return head->next;
}
};
sulution2 priority_queue
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<int, vector<int>, greater<int>>pq;// 加入了greater<int>表示为小顶堆
for (auto list:lists){
while(list){
pq.push(list->val);
list = list->next;
}
}
ListNode* head = new ListNode(0);
//ListNode* head = list;
while(!pq.empty()){
head->next = new ListNode(pq.top());
head = head->next;
pq.top();
}
return head->next;
}
};
这个方法和之前一样是个简单方法了 不过简单用了priority_queue 模板申明带3个参数:
priority_queue<Type, Container, Functional>,其中Type 为数据类型,Container为保存数据的容器,
Functional为元素比较方式。
此处Functional 为greater<int>,表示是一个小顶堆, 出栈先出最小的值
在新建一个链表将逐个小的值加入链表
sulution: 大佬写的牛鼻方法
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.empty()){
return nullptr;
}
map<int,int> map;
for(int i = 0; i < lists.size(); i++){
ListNode *head = lists[i];
while(head != nullptr){
map[head->val]++;
head = head->next;
}
}
ListNode *head = nullptr;
ListNode *p = nullptr;
for(auto it = map.begin(); it != map.end(); it++){
while(it->second != 0){
if(head == nullptr){
p = new ListNode(it->first);
head = p;
it->second--;
}else{
ListNode *temp = new ListNode(it->first);
p->next = temp;
p = p->next;
it->second--;
}
}
}
return head;
}
};