关于算法用现成数据结构和手撕

以215. 数组中的第K个最大元素 - 力扣(LeetCode)为例

用Java已有的大顶堆来解决,耗时如下

而如果手撕出来堆,结果则:

原因即:

PriorityQueue 是基于包装类 Integer、自动装箱、JDK 自带堆(底层数组 + 额外对象开销)

手写原地大顶堆,直接在原始 int 数组上操作,没有装箱拆箱、没有额外对象 ,所以快很多

具体代码如下:

java 复制代码
//Java已有堆
class Solution {
    public int findKthLargest(int[] nums, int k) {
        int len = nums.length;
        PriorityQueue<Integer> bigHeap = new PriorityQueue<Integer>(new Comparator<Integer>(){
            @Override
            public int compare(Integer num1,Integer num2){
                return num2 - num1;
            }
        });

        for(int i = 0 ; i < len ; i ++){
            bigHeap.offer(nums[i]); 
        }

        for(int i = 0 ; i < k - 1 ; i ++){
            bigHeap.poll(); 
        }

        return bigHeap.peek();
    }
}
java 复制代码
//纯手撕
class Solution {
    public int findKthLargest(int[] nums, int k) {
        int heapSize = nums.length;

        buildMaxHeap(nums,heapSize);

        for(int i = nums.length - 1;i >= nums.length - k + 1; i --){
            swap(nums,0,i);
            heapSize --;
            maxHeapify(nums,0,heapSize);
        }
        return nums[0];
    }

    public void buildMaxHeap(int[] a,int heapSize){
        for(int i = heapSize / 2 -1  ; i >= 0 ; i --){
            maxHeapify(a,i,heapSize);
        }
    }

    public void maxHeapify(int[] a, int i, int heapSize){
        int l = i * 2 + 1;
        int r = i * 2 + 2;
        int largest = i;

        if(l < heapSize && a[l] > a[largest]){
            largest = l;
        }
        if(r < heapSize && a[r] > a[largest]){
            largest = r;
        }
        if(largest != i){
            swap(a,i,largest);
            maxHeapify(a,largest,heapSize);
        }
    }

    public void swap(int[] a,int i ,int j){
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}
相关推荐
风落青芜9 小时前
C++入门:类和对象(二)
开发语言·数据结构·c++·算法
风落青芜10 小时前
C++入门:类和对象(三)
开发语言·数据结构·c++·算法
杜 硕12 小时前
从零开始的C++生活——第十五讲
开发语言·数据结构·c++·生活
小白的高手之路12 小时前
【LeetCode 热题 100】560. 和为 K 的子数组|前缀和、哈希表(Python)
数据结构·笔记·python·算法·leetcode·力扣·散列表
蓝色汪洋14 小时前
qsort使用指南
数据结构·算法
Code_Solitude16 小时前
c语言:期末复习5(二维数组)
c语言·数据结构·算法
charliejohn16 小时前
计算机考研 408 数据结构 散列表 平均查找长度 ASL
数据结构·散列表
charliejohn16 小时前
计算机考研 408 数据结构 图的概念及例题
数据结构
Shaoshing18 小时前
LeetCode100 32.最长有效括号
java·数据结构·leetcode
Lazyafei18 小时前
560. 和为 K 的子数组
数据结构·算法