优先级队列(堆)

已知父亲节点i——>孩子节点:2i+1 2i+2

已知孩子节点i——>父亲节点:(i-1)/2

代码示例:

package com.itbit_01;

import java.util.Arrays;

public class TestHeap {
    public int[] elem;
    public int usedSize;

    public TestHeap() {
        this.elem = new int[10];
    }

    public void adjustDown(int parent, int len) {
        int child = 2 * parent + 1;
        //首先判断是不是有左孩子
        while (child < len) {
            //是否有右孩子,如果有,child保存左右孩子最大值的下标
            if (child + 1 < len && this.elem[child] < this.elem[child + 1]) {
                child++;
            }
            //child当中存储的就是最大值的下标
            if (this.elem[child] > this.elem[parent]) {
                int temp = this.elem[child];
                this.elem[child] = this.elem[parent];
                this.elem[parent] = temp;
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    public void initHeap(int[] array) {
        for (int i = 0; i < array.length; i++) {
            this.elem[i] = array[i];
            this.usedSize++;
        }
        //建堆的时间复杂度是O(n*log2n)
        for (int i = (this.usedSize - 1 - 1) / 2; i >= 0; i--) {
            adjustDown(i, usedSize);
        }
    }

    public void adjustUp(int child) {
        int parent = (child - 1) / 2;
        while (child > 0) {
            if (this.elem[child] > this.elem[parent]) {
                int temp = this.elem[child];
                this.elem[child] = this.elem[parent];
                this.elem[parent] = temp;
                child = parent;
                parent = (child - 1) / 2;
            } else {
                break;
            }
        }
    }
    public void push(int val) {
        if (isFull()) {
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
        this.elem[this.usedSize] = val;
        this.usedSize++;
        adjustUp(this.usedSize-1);
    }
    public void pop() {
        if (isEmpty()) return;
        int temp = this.elem[0];
        this.elem[0] = this.elem[this.usedSize - 1];
        this.elem[this.usedSize - 1] = temp;
        this.usedSize--;
        adjustDown(0,this.usedSize);
    }

    public boolean isEmpty() {
        return this.usedSize == 0;
    }
    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }
    public void heapSort() {
        int end = this.usedSize - 1;
        while (end > 0) {
            int temp = this.elem[0];
            this.elem[0] = this.elem[end];
            this.elem[end] = temp;
            adjustDown(0,end);
            end--;
        }
    }
    public void show() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]+" ");
        }

    }
}

查找和最小的K对数字:

public static List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        List<List<Integer>> result = new LinkedList<>();
        PriorityQueue<List<Integer>> queue = new PriorityQueue<>(k, new Comparator<List<Integer>>() {
            @Override
            public int compare(List<Integer> o1, List<Integer> o2) {
                int sum1 = 0;
                int sum2 = 0;
                for (int i = 0; i < o1.size(); i++) {
                    sum1 += o1.get(i);
                }
                for (int i = 0; i < o2.size(); i++) {
                    sum2 += o2.get(i);
                }
                return sum2 - sum1;
            }
        });
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                List<Integer> list = new LinkedList<>();
                list.add(nums1[i]);
                list.add(nums2[j]);
                queue.offer(list);
                if (queue.size() > k) {
                    queue.poll();
                }
            }
        }
        while (!queue.isEmpty()) {
            List<Integer> list = new LinkedList<>();
            list = queue.poll();
            result.add(list);
        }
        List<List<Integer>> list1 = new LinkedList<>();
        for (int i = result.size() - 1; i >= 0; i--) {
            list1.add(result.get(i));
        }
        return list1;
    }

最后一块石头的重量:

public static int lastStoneWeight(int[] stones) {
        int len = stones.length;
        if (len == 1) return stones[0];
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for (int i = 0; i < len; i++) {
            queue.offer(stones[i]);
        }
        while (queue.size() > 1) {
            int y = queue.poll();
            int x = queue.poll();
            int sub = y - x;
            if (sub != 0) {
                queue.offer(sub);
            }
        }
        if (queue.size() == 0) return 0;
        return queue.peek();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值