zoukankan      html  css  js  c++  java
  • 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'. 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.
     1 class Solution {
     2 public:
     3     string frequencySort(string s) {
     4         map<char, int> m;
     5         for (int i = 0;i < s.size();i++)
     6         {
     7             m[s[i]]++;
     8         }
     9         map<char, int>::iterator ite;
    10         set<int> vet;
    11         for (ite = m.begin();ite != m.end();ite++)
    12         {
    13             vet.insert(ite->second);
    14         }
    15         set<int>::iterator ite1;
    16         vector<int> vet1;
    17         for (ite1 = vet.begin();ite1 != vet.end();ite1++)
    18         {
    19             vet1.push_back(*ite1);
    20         }
    21         sort(vet1.begin(), vet1.end());
    22         string str = "";
    23         for (int i = vet1.size() - 1;i >= 0;i--)
    24         {
    25             for (ite = m.begin();ite != m.end();ite++)
    26             {
    27                 if (ite->second == vet1[i])
    28                 {
    29                     for (int j = 0;j < vet1[i];j++)
    30                     {
    31                         str += ite->first;
    32                     }
    33                 }
    34             }
    35         }
    36         return str;
    37     }
    38 };
     1 #include<iostream>
     2 #include<vector>
     3 #include<string>
     4 #include<map>
     5 #include<algorithm>
     6 #include<set>
     7 using namespace std;
     8 class Solution {
     9     public:
    10         string frequencySort(string s) {
    11                 vector<vector<int>> pairs(256, vector<int>(2,1));
    12                 for (int i = 0; i < pairs.size(); i++) {
    13                     pairs[i][1] = i;
    14                 }
    15                 for (char c : s)
    16                     pairs[c][0]++;
    17                 string ret;
    18                 sort(pairs.begin(), pairs.end());
    19                 
    20                 for (int i = (int)pairs.size() - 1; i >= 0; i--) {
    21                     for (int j = 0; j < pairs[i][0]; j++)
    22                         ret += (char)pairs[i][1];
    23                 }
    24                 return ret;
    25             }
    26         };
    27 int main()
    28 {
    29     Solution s;
    30     string str = "tree";
    31     cout << s.frequencySort(str) << endl;
    32     system("pause");
    33     return 0;
    34 }
  • 相关阅读:
    ORACLE数据库找回用户密码
    PO、POJO、BO、DTO、VO之间的区别(转)
    Http报头Accept与Content-Type的区别
    java.lang.IllegalStateException: getWriter() has already been called for this response
    利用策略模式实现了同一接口的多个Servicel实现类,如何同时注入Controller
    java.util.Stack类简介
    java为什么要重写hashCode和equals方法?
    PowerDesigner15连接Oracle数据库并导出Oracle的表结构
    解决ODBC连接Oracle数据库报Unable to connect SQLState=08004问题
    IIS 返回 405
  • 原文地址:https://www.cnblogs.com/wujufengyun/p/7465927.html
Copyright © 2011-2022 走看看