zoukankan      html  css  js  c++  java
  • 算法题:堆排序的应用

    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();
                }
            });
  • 相关阅读:
    sql server 总结一下堆和索引的东西
    公开课笔记:美联储与经济危机(一:金本位,大萧条)
    关于一个springcloud项目中学到的(二:分布式集群,Maven,JDKJRE,编译和解释)
    关于一个springcloud项目中学到的(一:感受)
    sql 经典查询50题 思路(一)
    信息熵的定义和简单计算
    使用Mulesoft建立webservice, jax-ws方式, wsdl first
    使用Mulesoft建立webservice, simple方式,POJO
    编写高质量代码 改善Python程序的91个建议
    23 种编程学习资料免费分享,你想要的都有
  • 原文地址:https://www.cnblogs.com/jingpeng77/p/14472931.html
Copyright © 2011-2022 走看看