Day75x.算法训练

23. 合并 K 个升序链表

/**
 * 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;
        }
    }
}

347. 前 K 个高频元素

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;
    }
}

234. 回文链表

/**
 * 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;
        }
}

  • 18
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EVE(伊娃)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值