leetcode 451. Sort Characters By Frequency
来源:互联网 发布:农村淘宝快递网点查询 编辑:程序博客网 时间:2024/06/07 12:39
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.
题意很简单,就是一个简单的排序,
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>using namespace std;bool cmp(pair<char, int> a , pair<char, int> b){ return a.second > b.second;}class Solution {public: string frequencySort(string s) { map<char, int> mmp; for (char a : s) { if (mmp.find(a) == mmp.end()) mmp[a] = 1; else mmp[a] += 1; } vector<pair<char, int>> res(mmp.begin(),mmp.end()); sort(res.begin(), res.end(), cmp); string tmp = ""; for (pair<char, int> a : res) { for (int i = 0; i < a.second; i++) tmp += a.first; } return tmp; }};
阅读全文
0 0
- LeetCode 451. Sort Characters By Frequency
- [LeetCode]451. Sort Characters By Frequency
- [leetcode] 451. Sort Characters By Frequency
- [LeetCode]451. Sort Characters By Frequency
- LeetCode笔记:451. Sort Characters By Frequency
- LeetCode 451. Sort Characters By Frequency
- 【LeetCode】 451. Sort Characters By Frequency
- [leetcode]451. Sort Characters By Frequency
- Leetcode 451. Sort Characters By Frequency
- Leetcode 451. Sort Characters By Frequency
- Leetcode-451. Sort Characters By Frequency
- LeetCode 451. Sort Characters By Frequency
- LeetCode 451. Sort Characters By Frequency
- LeetCode OJ 451. Sort Characters By Frequency
- leetcode题解-451. Sort Characters By Frequency
- 【Leetcode】451. Sort Characters By Frequency
- LeetCode 451. Sort Characters By Frequency
- leetcode 451. Sort Characters By Frequency
- 洛谷P1199 三国游戏(博弈)
- Android移动开发-在Android项目里调用基于高德地图API实现定位
- jsp学习6-jsp的page指令
- uboot中PCIe驱动程序说明
- 软工视频——软件工程
- leetcode 451. Sort Characters By Frequency
- Centos7上安装使用Git
- Clojure Reactive Programming.pdf 英文原版 免费下载
- A10_委托基础回顾
- Java RMI Demo
- 实验3 系统调用
- hexo 首页文章只显示一部分
- Linux:(多摄像头)如何运行指定的摄像头
- jvm 判断对象是否已经死亡