451. Sort Characters By Frequency

Posted 无惧风云

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于451. Sort Characters By Frequency的主要内容,如果未能解决你的问题,请参考以下文章

451. Sort Characters By Frequency

451. Sort Characters By Frequency

451. Sort Characters By Frequency

451. Sort Characters By Frequency

leetcode 451. Sort Characters By Frequency

[leetcode-451-Sort Characters By Frequency]