-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
148 lines (121 loc) · 3.27 KB
/
Solution.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package L23;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
class Node implements Comparable<Node> {
int key;
ListNode listNode;
public Node(int key, ListNode listNode) {
this.key = key;
this.listNode = listNode;
}
@Override
public int compareTo(Node o) {
return this.key - o.key;
}
}
/**
* 小根堆
*/
class BinaryHeap {
private List<Node> heap;
public BinaryHeap() {
this.heap = new ArrayList<>();
heap.add(null);
}
public boolean isEmpty() {
return heap.size() == 1;
}
public Node getMin() {
return heap.get(1);
}
public void insert(Node node) {
heap.add(node);
heapifyUp(heap.size() - 1);
}
public void deleteMin() {
heap.set(1, heap.get(heap.size() - 1));
heap.remove(heap.size() - 1);
heapifyDown(1);
}
private void heapifyUp(int p) {
while (p > 1) {
if (heap.get(p).key < heap.get(p / 2).key) {
swap(p, p / 2);
p /= 2;
} else break;
}
}
private void heapifyDown(int p) {
int child = p * 2;
while (child < heap.size()) {
int other = p * 2 + 1;
if (other < heap.size() && heap.get(other).key < heap.get(child).key)
child = other;
if (heap.get(p).key > heap.get(child).key) {
swap(child, p);
p = child;
child = p * 2;
} else break;
}
}
private void swap(int i, int j) {
Node temp = heap.get(i);
heap.set(i,heap.get(j));
heap.set(j,temp);
}
}
public class Solution {
private PriorityQueue<Node> queue = new PriorityQueue<>();
public ListNode mergeKLists(ListNode[] lists) {
for (ListNode listNode : lists) {
if (listNode == null) continue;
queue.add(new Node(listNode.val, listNode));
}
ListNode head = new ListNode();// 保护点
ListNode tail = head;
while (!queue.isEmpty()) {
Node node = queue.poll();
tail.next = node.listNode;
tail = tail.next;
ListNode next = node.listNode.next;
if (next != null) {
queue.add(new Node(next.val, next));
}
}
return head.next;
}
private BinaryHeap q = new BinaryHeap();
public ListNode mergeKLists2(ListNode[] lists) {
for (ListNode listNode : lists) {
if (listNode == null) continue;
q.insert(new Node(listNode.val, listNode));
}
ListNode head = new ListNode();// 保护点
ListNode tail = head;
while (!q.isEmpty()) {
Node node = q.getMin();
q.deleteMin();
tail.next = node.listNode;
tail = tail.next;
ListNode next = node.listNode.next;
if (next != null) {
q.insert(new Node(next.val, next));
}
}
return head.next;
}
}