Leetcode 451. Sort Characters By Frequency JAVA語言

來源:互聯網
上載者:User

標籤:java   appears   before   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‘. 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,消化不了。。。

Leetcode 451. Sort Characters By Frequency JAVA語言

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.