算法题:堆排序的应用

1.最小的k个数

import java.util.*;
public class Solution {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> list = new ArrayList<>();
        if(input.length==0||k>input.length)return list;
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>(){
            @Override
            public int compare(Integer o1,Integer o2){
                return o1.compareTo(o2);
            }
        });
        for(int i=0;i<input.length;i++){
            queue.offer(input[i]);
        }     
        for(int i=0;i<k;i++){
            list.add(queue.poll());
        }
        return list;
    }
}

2.出现次数的TopK问题

给定String类型的数组strArr,再给定整数k,请严格按照排名顺序打印 出次数前k名的字符串。

import java.util.*;
class pair{
    int count;
    String word;
    pair(int cnt,String key){
        count = cnt;
        word = key;
    }
}
class UDComparator implements Comparator<pair>{
    @Override
    public int compare(pair p1,pair p2){
        if(p1.count==p2.count){
            return p1.word.compareTo(p2.word);
        }
        return p2.count-p1.count;
    }
}

public class Solution {
    /**
     * return topK string
     * @param strings string字符串一维数组 strings
     * @param k int整型 the k
     * @return string字符串二维数组
     */
    
    public String[][] topKstrings (String[] strings, int k) {
        // write code here
        HashMap<String,Integer> map = new HashMap<>();
        for(String s:strings){
            if(map.containsKey(s)){
                map.put(s,map.get(s)+1);
            }else{
                map.put(s,1);
            }
        }
        PriorityQueue<pair> queue = new PriorityQueue<>(new UDComparator());
        for(String key:map.keySet()){
            queue.offer(new pair(map.get(key),key));
        }
        String[][] result = new String[k][2];
        for(int i=0;i<k;i++){
            pair p = queue.poll();
            result[i][0] = p.word;
            result[i][1] = String.valueOf(p.count);
        }
        return result;
    }
}

3.合并k个已排序的链表

合并 k 个已排序的链表并将其作为一个已排序的链表返回。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
import java.util.*;
public class Solution {
    public ListNode mergeKLists(ArrayList<ListNode> lists) {
        if(lists.size()==0)return null;
        PriorityQueue<ListNode> queue = new PriorityQueue<>(new Comparator<ListNode>(){
            @Override
            public int compare(ListNode o1,ListNode o2){
                Integer head1 = o1.val;
                Integer head2 = o2.val;
                return head1.compareTo(head2);
            }
        });
        for(ListNode L:lists){
            if(L!=null){
                queue.offer(L);
            }
        }
        ListNode dummy = new ListNode(-1);
        ListNode res = dummy;
        while(!queue.isEmpty()){
            ListNode LinkheadNode = queue.poll();
            res.next = LinkheadNode;
            res = res.next;
            LinkheadNode = LinkheadNode.next;
            if(LinkheadNode!=null){
                queue.offer(LinkheadNode);
            }
        }
        return dummy.next;
    }
}

 4.对Map类型的数据进行自定义排序

根据<key,value>中的value升序排序

PriorityQueue<Map.Entry<Integer, Integer>> priorityQueue = new PriorityQueue<>(
        new Comparator<Map.Entry<Integer, Integer>>() {
            public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) {
                return e1.getValue() - e2.getValue();
            }
        });
原文地址:https://www.cnblogs.com/jingpeng77/p/14472931.html