/**
* Definition for singly-linked list.
* public 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 Solution {
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<ListNode> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(o -> o.val));
for (int i = 0; i < lists.length; i++) {
if (lists[i] != null) {
priorityQueue.offer(lists[i]);
}
}
ListNode s = new ListNode();
ListNode t = s;
while (!priorityQueue.isEmpty()) {
ListNode p = priorityQueue.poll();
t.next = p;
t = p;
if (p.next != null) {
priorityQueue.offer(p.next);
}
}
return s.next;
}
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue priorityQueue = new PriorityQueue(lists.length);
for (ListNode node : lists) {
if (node != null) {
priorityQueue.offer(node);
}
}
System.out.println(Arrays.toString(priorityQueue.array));
ListNode dummyHead = new ListNode(-1);
ListNode p = dummyHead;
while (true) {
if (priorityQueue.isEmpty()) {
break;
}
ListNode poll = priorityQueue.poll();
if (poll.next != null) {
priorityQueue.offer(poll.next);
}
p.next = poll;
p = poll;
}
return dummyHead.next;
}
class PriorityQueue {
private ListNode[] array;
int size = 0;
int capacity;
public PriorityQueue(int capacity) {
this.array = new ListNode[capacity];
this.capacity = capacity;
}
public boolean offer(ListNode e) {
if (isFull()) {
return false;
}
int c = size;
int p = (c - 1) / 2;
while (c > 0 && e.val < array[p].val) {
array[c] = array[p];
c = p;
p = (c - 1) / 2;
}
array[c] = e;
size++;
return true;
}
public boolean isFull() {
return size == capacity;
}
public ListNode peek() {
if (size == 0) {
return null;
}
return array[0];
}
public ListNode poll() {
if (size == 0) {
return null;
}
swap(0, size - 1);
ListNode res = array[size - 1];
array[size - 1] = null;
size--;
down(0);
return res;
}
private void down(int p) {
int left = (2 * p) + 1;
int right = left + 1;
int min = p;
if (left < size && array[min].val > array[left].val) {
min = left;
}
if (right < size && array[min].val > array[right].val) {
min = right;
}
if (min != p) {
swap(p, min);
down(min);
}
}
public boolean isEmpty() {
return size == 0;
}
public void swap(int a, int b) {
ListNode c = array[a];
array[a] = array[b];
array[b] = c;
}
}
}
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
if (map.containsKey(num)) {
Integer oldVal = map.get(num);
map.put(num, oldVal + 1);
} else {
map.put(num, 1);
}
}
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(map::get));
for (Integer key : map.keySet()) {
priorityQueue.offer(key);
if (priorityQueue.size() > k) {
priorityQueue.poll();
}
}
int[] result = new int[k];
while (!priorityQueue.isEmpty()) {
result[--k] = priorityQueue.poll();
}
return result;
}
}
/**
* Definition for singly-linked list.
* public 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 Solution {
public boolean isPalindrome(ListNode head) {
ListNode fast = head;
ListNode slow = head;
// 找到中间节点
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
// 反转链表
ListNode pre = null;
ListNode midNode = slow;
while (midNode != null) {
ListNode tempNext = midNode.next;
midNode.next = pre;
pre = midNode;
midNode = tempNext;
}
while (pre != null) {
if (pre.val != head.val) {
return false;
}
pre = pre.next;
head = head.next;
}
return true;
}
}