题解 | #【模板】堆#

【模板】堆

http://www.nowcoder.com/practice/13f61c8c92404f5ea5d6fa4c692869fb

注意事项:

  1. 除了 top 操作,push 操作和 pop 操作都要进行 堆的调整。你在 插入 一个新数据,亦或者是 弹出 堆顶元素,都别忘记 调整堆的结构。无论何时,你用来存储 大根堆 的结构,都一定要满足大根堆的性质
  2. 对于当前代码,可以进行 优化。优化的地方,就是每次 弹出 堆顶元素之后,不要 删除 该位置上的元素,而是先将 堆顶元素堆尾元素 进行交换,再删除该元素。因为对于 ArrayList 结构来说,每一次 remove,如果 remove 的位置向后有许多元素,都需要进行移动,最坏情况下n长度的数组,移除 下标为0 位置上的元素,需要移动 n-1 个元素。
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = Integer.valueOf(scan.nextLine().trim());
        ArrayList<String> ans = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String[] operator = scan.nextLine().split(" ");
            if ("push".equals(operator[0])) {
                push(Integer.valueOf(operator[1]));
            } else if ("top".equals(operator[0])) {
                ans.add(top());
            } else {
                ans.add(pop());
            }
        }
        for (String str : ans) {
            System.out.println(str);
        }
    }
    public static ArrayList<Integer> heap = new ArrayList<>();
    public static void push(int x) {
        heap.add(x);
        heapInsert(heap.size() - 1);
    }
    public static String top() {
        return heap.isEmpty() ? "empty" : String.valueOf(heap.get(0));
    }
    public static String pop() {
        if (heap.isEmpty()) {
            return "empty";
        } else {
            String str = String.valueOf(heap.get(0));
            swap(0, heap.size() - 1);
            heap.remove(heap.size() - 1);
            heapify(0, heap.size());
            return str;
        }
    }
    public static void heapInsert(int index) {
        while (heap.get(index) > heap.get((index - 1) / 2)) {
            swap(index, (index - 1) / 2);
            index = (index - 1) / 2;
        }
    }
    public static void heapify(int index, int heapSize) {
        int left = index * 2 + 1;
        while (left < heapSize) {
            int largest = left + 1 < heapSize && heap.get(left + 1) > heap.get(left) ? left + 1 : left;
            largest = heap.get(largest) > heap.get(index) ? largest : index;
            if (largest == index) {
                break;
            }
            swap(largest, index);
            index = largest;
            left = index * 2 + 1;
        }
    }
    public static void swap(int p1, int p2) {
        int tmp = heap.get(p1);
        heap.set(p1, heap.get(p2));
        heap.set(p2, tmp);
    }
}
全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-20 17:10

相关推荐

烤点老白薯:亲娘嘞🐶💩啊你的简历
点赞 评论 收藏
分享
当年还在美团那个倒霉的&nbsp;Peppr&nbsp;团队工作时,我一直有个疑问:这群人每天到底在自嗨什么。每次开会一堆人围着一堆“看起来很高级”的文档转,模板统一、名词复杂、页数感人,每一页都在暗示一件事:“你不懂,是因为你不专业。”但现实是——代码照样写在&nbsp;💩&nbsp;山上,该出问题还是会出问题,这真的很逗,系统一出问题,文档的唯一作用就是证明:“我们当初确实认真写过文档。”所以本质区别到底是什么?是代码质量提升了,还是大家在精神层面完成了一次“工程师&nbsp;cosplay”?有句话说得好潮水退去才知道谁在裸泳。还记得当时的马哥、明哥(图&nbsp;1&nbsp;左)最爱反复强调一句话:“所有场景一定要想到。”、“这个场景为什么没考虑到?”不过他们这些话我是真的听进去了。不然我也不会在一年多前就说:这个项目活不过两年。顺带一提,那段时间还有个固定节目。每次下楼,总能听见我明哥在吐槽不同的人。我从他身后绕过去,经常能听到他一边抽烟一边说:“xx&nbsp;这小子太坑了,回头我一定要跟马哥说说。”于是深谙人情世故但真不会抽烟的我也会从口袋掏出一支低尼古丁含量的烟给自己点上,假意自己什么都没听到什么都不知道,只是来抽烟的。后来我才明白,这可能也是团队文化的一部分:问题永远在别人身上,而我们,永远在复盘里😂。
秋招白月光
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务