• 【LeetCode】Day122-根据字符出现频率排序 & 最接近原点的 K 个点


    题目1、据字符出现频率排序

    451. 根据字符出现频率排序【中等】

    题解

    优先队列

    和昨天的题非常像,不同是昨天是统计数字的频次,今天统计字符的频次

    思路很容易,同样用优先队列实现,关键是如何用代码实现,PriorityQueue中的元素为Map.Entry

    class Solution {
        public String frequencySort(String s) {
            //计数
            int n=s.length();
            Map<Character,Integer>map=new HashMap<>();
            for(int i=0;i<n;i++){
                map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1);
            }
            //优先队列初始化
            Queue<Map.Entry<Character,Integer>>queue=new PriorityQueue<>(new Comparator<Map.Entry<Character,Integer>>(){
                public int compare(Map.Entry<Character,Integer>p1,Map.Entry<Character,Integer>p2){
                    return p2.getValue()-p1.getValue();
                }
            });
            //频次存储进优先队列
            for(Map.Entry<Character,Integer>entry:map.entrySet()){
                queue.offer(entry);
            }
            //重排字符串
            StringBuffer sb=new StringBuffer();
            while(!queue.isEmpty()){
                char ch=queue.poll().getKey();
                int count=map.get(ch);
                for(int i=0;i<count;i++)
                    sb.append(ch);
            }
            return sb.toString();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    时间复杂度: O ( n + k ) O(n+k) O(n+k),其中 n 是字符串 s 的长度,k 是字符串 s 包含的不同字符的个数。

    空间复杂度: O ( n ) O(n) O(n)

    桶排序

    由于每个字符在字符串中出现的频率存在上限,因此可以使用桶排序的思想,根据出现次数生成排序后的字符串。具体做法如下:

    遍历字符串,统计每个字符出现的频率,同时记录最高频率 maxFreq;

    创建桶,存储从 1 到 maxFreq 的每个出现频率的字符;

    按照出现频率从大到小的顺序遍历桶,对于每个出现频率,获得对应的字符,然后将每个字符按照出现频率拼接到排序后的字符串。

    class Solution {
        public String frequencySort(String s) {
            //计数
            int n=s.length(),maxfrq=0;
            Map<Character,Integer>map=new HashMap<>();
            for(int i=0;i<n;i++){
                int frq=map.getOrDefault(s.charAt(i),0)+1;
                map.put(s.charAt(i),frq);
                maxfrq=Math.max(maxfrq,frq);
            }
            //桶初始化
            StringBuffer[] bucket=new StringBuffer[maxfrq+1];
            for(int i=0;i<=maxfrq;i++){
                bucket[i]=new StringBuffer();
            }
            //桶存储每个出现频率的字符
            for(Map.Entry<Character,Integer>entry:map.entrySet()){
                char ch=entry.getKey();
                int frq=entry.getValue();
                bucket[frq].append(ch);
            }
            //重排字符串
            StringBuffer sb=new StringBuffer();
            for(int i=maxfrq;i>0;i--){//依照频次遍历桶
                for(int j=0;j<bucket[i].length();j++){//遍历桶中字符
                    for(int k=0;k<i;k++)//依照字符出现频次加入字符串
                        sb.append(bucket[i].charAt(j));
                }
            }
            return sb.toString();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    时间复杂度: O ( n + k ) O(n+k) O(n+k),其中 n 是字符串 s 的长度,k 是字符串 s 包含的不同字符的个数。
    创建桶并将不同字符加入桶需要 O(k) 的时间。
    生成排序后的字符串,需要 O(k) 的时间遍历桶,以及 O(n) 的时拼接字符串时间。

    空间复杂度: O ( n + k ) O(n+k) O(n+k),空间复杂度主要取决于桶和生成的排序后的字符串。

    题目2、最接近原点的 K 个点

    973. 最接近原点的 K 个点【中等】

    题解

    优先队列

    思路很简单,同样用优先队列实现,优先队列元素是 int[3]{x,y,distance},实现一个大根堆

    class Solution {
        public int[][] kClosest(int[][] points, int k) {
            int n=points.length;
            PriorityQueue<int[]>queue=new PriorityQueue<int[]>(new Comparator<int[]>(){
                public int compare(int[] a,int[] b){
                    return b[2]-a[2];
                }
            });
            for(int i=0;i<n;i++){
                int x=points[i][0],y=points[i][1];
                int dis=x*x+y*y;
                if(queue.size()==k){
                    if(queue.peek()[2]>dis){
                        queue.poll();
                        queue.offer(new int[]{x,y,dis});
                    }
                }
                else
                    queue.offer(new int[]{x,y,dis});
            }
            int[][] res=new int[k][2];
            for(int i=0;i<k;i++){
                int[] point=queue.poll();
                res[i][0]=point[0];
                res[i][1]=point[1];
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    时间复杂度: O ( n l o g k ) O(nlogk) O(nlogk),其中 n 是 points 的长度。由于大根堆维护的是前 k 个距离最小的点,因此弹出和插入操作的单次时间复杂度均为 O(logk)。在最坏情况下,数组里 n 个点都会插入,因此时间复杂度为 O(nlogk)。

    空间复杂度: O ( k ) O(k) O(k),优先队列里最多存储k个点。

    直接排序

    这题还有更简单的方法,直接对数组排序就可以…最后取出数组的前k个元素

    class Solution {
        public int[][] kClosest(int[][] points, int k) {
            Arrays.sort(points,new Comparator<int[]>(){
                public int compare(int[] p1,int[] p2){
                    return (p1[0]*p1[0]+p1[1]*p1[1])-(p2[0]*p2[0]+p2[1]*p2[1]);
                }
            });
            return Arrays.copyOfRange(points,0,k);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn)

    空间复杂度: O ( l o g n ) O(logn) O(logn),排序所需额外的空间复杂度为 O(logn)。

  • 相关阅读:
    Vue理解01
    vue常见的keep-alive问题
    以太网交换机的两种转发方式
    java通过解密身份证计算年龄(精确到日)
    Error: Cannot install in Homebrew on ARM processor in Intel default prefix
    【算法设计与分析】— —实现最优载的贪心算法
    01_SpringBoot简介及项目搭建
    uniapp中使用render.js进行openers、arcgis等地图操作
    day59 单调栈.p2
    141、★并查集-LeetCode-冗余连接Ⅰ和Ⅱ
  • 原文地址:https://blog.csdn.net/qq_43417265/article/details/126598111