Leetcode 347 前K个高频元素 topK问题手写大顶堆

  JAVA:

public final int[] topKFrequent(int[] nums, int k) {
        Item[] arr = this.initItemArr(nums);
        HeapSort heap = new HeapSort(arr);
        int[] reArr = new int[k];
        for (int i = 0; i < k; i++) reArr[i] = heap.getHead().getVal();
        return reArr;
    }

    private final Item[] initItemArr(int[] nums) {
        Map<Integer, Item> map = new HashMap<Integer, Item>();
        for (int i = 0; i < nums.length; i++) {
            int val = nums[i];
            if (!map.containsKey(val)) map.put(val, new Item(val, 0));
            Item item = map.get(val);
            item.setNum(item.getNum() + 1);
        }
        Item[] arr = new Item[map.keySet().size()];
        Iterator<Integer> itemIterator = map.keySet().iterator();
        int i = 0;
        while (itemIterator.hasNext()) {
            arr[i] = map.get(itemIterator.next());
            i++;
        }
        return arr;
    }


    private class Item {
        private int val;
        private int num;

        Item(int val, int num) {
            this.val = val;
            this.num = num;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }

    private class HeapSort {
        HeapSort(Item[] arr) {
            this.arr = arr;
            this.len = arr.length;
            for (int i = len - 1; i >= 0; i--) this.sort(arr, i);
        }

        Item[] arr;
        int len;

        public Item getHead() {
            if (this.len == 0) return null;
            Item ans = this.arr[0];
            this.arr[0] = null;
            this.exchange(this.arr, 0, this.len - 1);
            this.len--;
            this.sort(arr, 0);
            return ans;
        }

        private final void sort(Item[] arr, int point) {
            Item root = arr[point];
            if (root == null) return;
            int leftPoint = point * 2 + 1, rightPoint = point * 2 + 2, len = arr.length;
            int max = root.getNum(), next = point;
            if (leftPoint < this.len && arr[leftPoint].getNum() > max) {
                max = arr[leftPoint].getNum();
                next = leftPoint;
            }
            if (rightPoint < this.len && arr[rightPoint].getNum() > max) {
                max = arr[rightPoint].getNum();
                next = rightPoint;
            }
            if (next != point) {
                this.exchange(arr, point, next);
                this.sort(arr, next);
            }
        }

        private final void exchange(Item[] arr, int point0, int point1) {
            Item temp = arr[point0];
            arr[point0] = arr[point1];
            arr[point1] = temp;
        }
    }

  JS:

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var topKFrequent = function (nums, k) {
    let arr = initArr(nums), heap = new HeapSort(arr), ans = [];
    for (let i = 0; i < k; i++) ans.push(heap.getHead().val);
    return ans;
};

var initArr = function (arr) {
    let map = new Map();
    for (let i = 0; i < arr.length; i++) {
        if (!map.get(arr[i])) map.set(arr[i], new Item(arr[i], 0));
        map.get(arr[i]).num = map.get(arr[i]).num + 1;
    }
    let ans = [];
    map.forEach(function (value, key) {
        ans.push(value);
    });
    return ans;
}

var HeapSort = function (arr) {
    if (!arr || arr.length == 0) throw new Error("arr is null!");
    this.arr = arr;
    this.len = arr.length;

    this.getHead = function () {
        if (this.len == 0) throw new Error("out of arr index!");
        let head = this.arr[0];
        this.arr[0] = null;
        this.exchange(0, this.len - 1);
        this.len--;
        this.sort(0);
        return head;
    }

    this.sort = function (point) {
        if (point >= this.len) return;
        let root = this.arr[point];
        if (!root) return;
        let leftPoint = 2 * point + 1, rightPoint = 2 * point + 2;
        let max = root.num, nextPoint = point;
        if (leftPoint < this.len && this.arr[leftPoint].num > max) {
            max = this.arr[leftPoint].num;
            nextPoint = leftPoint;
        }
        if (rightPoint < this.len && this.arr[rightPoint].num > max) {
            max = this.arr[rightPoint].num;
            nextPoint = rightPoint;
        }
        if (nextPoint != point) {
            this.exchange(point, nextPoint);
            this.sort(nextPoint);
        }
    }

    this.exchange = function (point0, point1) {
        let temp = this.arr[point0];
        this.arr[point0] = this.arr[point1];
        this.arr[point1] = temp;
    }

    for (let i = this.len - 1; i >= 0; i--) this.sort(i);
}

var Item = function (val, num) {
    this.val = val;
    this.num = num;
}

posted @ 2021-09-08 10:46  牛有肉  阅读(146)  评论(0编辑  收藏  举报