Leetcode 451. Sort Characters By Frequency JAVA语言

Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:"tree"Output:"eert"Explanation:‘e‘ appears twice while ‘r‘ and ‘t‘ both appear once.
So ‘e‘ must appear before both ‘r‘ and ‘t‘. Therefore "eetr" is also a valid answer.

Example 2:
Input:"cccaaa"Output:"cccaaa"Explanation:Both ‘c‘ and ‘a‘ appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.

Example 3:
Input:"Aabb"Output:"bbAa"Explanation:"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that ‘A‘ and ‘a‘ are treated as two different characters.

题意:将字符串中字符按出现频率输出。

public class Solution {
    public String frequencySort(String s) {
        if(s==null || s.length()<=2)return s;
        int length=s.length();
        //统计各个字符出现的次数
        HashMap<Character,Integer> map=new HashMap<Character,Integer>();
        for(int i=0;i<length;i++){
            char c=s.charAt(i);
            if(map.containsKey(c)){
                map.put(c,map.get(c)+1);
            }else{
                map.put(c,1);
            }
        }
        //sb1求出各种频率的字符
        //"eeeee"的时候,e出现了length次,所以申请的时候length+1
        StringBuilder[] sb1=new StringBuilder[length+1];
        int max=0;
        for(char c : map.keySet()){
            int fre=map.get(c);
            if(sb1[fre]==null){
                sb1[fre]=new StringBuilder();
            }
            if(fre>max)max=fre;
            for(int i=0;i<fre;i++){
                sb1[fre].append(c);
            }
        }
        //最后ret把各种频率的字符由高到低连接起来
        StringBuilder ret=new StringBuilder();
        for(int i=max;i>0;i--){
            if(sb1[i]!=null)
            ret.append(sb1[i]);
        }
        return ret.toString();
        //方法2;二维数组
        int[][] count=new int[128][2];
        char[] ch=s.toCharArray();
        for(char c :ch){
            count[c][0]=c;
            count[c][1]++;
        }
        Arrays.sort(count,new Comparator<int[]>(){
                public int compare(int[] a,int []b){
                    return b[1]-a[1];
                }
            });
        StringBuilder ret=new StringBuilder();
        for(int i=0;i<128;i++){
            for(int j=0;j<count[i][1];j++){
                ret.append((char)count[i][0]);
            }
        }
        return ret.toString();
    }
}

PS:计算各个字符频率,拼接。。。

群里大神说有用heap,消化不了。。。

时间: 2024-12-15 21:47:52

Leetcode 451. Sort Characters By Frequency JAVA语言的相关文章

LeetCode 451. Sort Characters By Frequency 根据字符出现频率排序 (C++/Java)

题目: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'

451. Sort Characters By Frequency

Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Th

[LC] 451. Sort Characters By Frequency

Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Th

【LEETCODE】:Sort Characters By Frequency

声明:该题目来自https://github.com/soulmachine, 一.Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you mu

Leetcode 148. Sort List 归并排序 in Java

148. Sort List Total Accepted: 81218 Total Submissions: 309907 Difficulty: Medium Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *

Leetcode 503. Next Greater Element II JAVA语言

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the ar

Leetcode 371. Sum of Two Integers JAVA语言

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. 题意:计算a+b,但是不许使用+和- public class Solution {     public int getSum(int a, int b) {         //第一种         while(a!=

Leetcode 203. Remove Linked List Elements JAVA语言

Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 题意:删除链表中的节点 /**  * Definition for singly-linked list.  * 

LeetCode算法题-Single Number(Java实现)

这是悦乐书的第175次更新,第177篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第34题(顺位题号是136).给定一个非空的整数数组,除了一个元素外,每个元素都会出现两次. 找到那个只出现了一次的元素.例如: 输入:[2,2,1] 输出:1 输入:[4,1,2,1,2] 输出:4 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02 第一种解法 因为已经限定传入的数组不为空,所以此题不需要