栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JavaSE

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaSE

文章目录
  • 1.Java堆模拟实现
  • 2.TopK问题

1.Java堆模拟实现
import java.util.Arrays;



class Heap{
    public int[]array;
    int useSize=0;
    private  void swap(int[]array,int left,int right){
        int tmp=array[left];
        array[left]=array[right];
        array[right]=tmp;
    }
    private void AdjustDown(int root){
        //向下调整,每次调整的边界都是数组的大小,超过数组大小停止调整
        int child=2*root+1;
        while(child< useSize){
            //存在叶子节点,找到最大值
            if(child+1< useSize&&array[child]
                child++;
            }
            if(array[root]
                swap(array,root,child);
            }
            else{
                break;
            }
            root=child;
            child=2*root+1;
        }
    }
    Heap(int[]_array){ //创建大根堆
        this.array=_array.clone();
        useSize=this.array.length;
        for(int parent=(array.length-1)/2;parent>=0;parent--){
            AdjustDown(parent);
        }
    }
    //入队,保证堆仍然为大根堆
    private void grow(){
        this.array=Arrays.copyOf(this.array,2*this.array.length);
    }
    //向上调整
    private void AdjustUp(int child){
        int root=(child-1)/2;
        while(root>=0){
            if(this.array[root]
                swap(this.array,root,child);
            }
            else{
                break;
            }
            child=root;
            root=(child-1)/2;
        }
    }
    public void add(int num){
        if(this.useSize==this.array.length){
            grow();
        }
        this.array[useSize]=num;
        AdjustUp(useSize);
        useSize++;
    }
    //出堆
    public boolean isEmpty(){
        return useSize==0;
    }
    public int poll(){
        if(isEmpty()){
            throw new RuntimeException("堆为空");
        }
        int ret=this.array[0];
        swap(this.array,0,this.useSize-1);
        this.array[this.useSize-1]=0;
        this.useSize--;
        AdjustDown(0);
        return ret;
    }
    @Override
    public String toString() {
        StringBuilder str=new StringBuilder();
        for(int i=0;i
            str.append(this.array[i]).append(" ");
        }
        return str.toString();
    }
}

public class TestHeap {
    public static void main(String[] args) {
        int[]arr={27,15,19,18,28,34,65,49,25,37};
        Heap heap=new Heap(arr);
        System.out.println(heap);
        System.out.println(heap.useSize);
        heap.add(100);
        System.out.println(heap);
        System.out.println(heap.poll());
        System.out.println(heap);
        System.out.println(heap.useSize);
    }
}
2.TopK问题

求前K个最小的元素,需要建立大小为K的大堆。
求前K个最大的元素,需要建立大小为K的小堆。

这里以求前K个最小的元素为例

class TopK{
    static int[] topK(int[]array,int k){//找前k个最小的元素
        PriorityQueuemaxHeap=new PriorityQueue<>(k, new Comparator() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });//建大堆
        //遍历数组,前K个元素放到队列中
        for(int i=0;i
            if(maxHeap.size()
                maxHeap.offer(array[i]);
            }
            else{
                if(array[i]
                    maxHeap.poll();
                    maxHeap.offer(array[i]);
                }
            }
        }
        int[]ret=new int[maxHeap.size()];
        for(int i=0;i
            ret[i]=maxHeap.poll();
        }
        return ret;
    }
}

public class TestHeap {
    public static void main(String[] args) {
        int[]arr={27,15,19,18,28,34,65,49,25,37};
        System.out.println(Arrays.toString(TopK.topK(arr, 4)));
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966500.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号