451. Sort Characters By Frequency (sort map)
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.
Approach #1: c++.
class Solution {
public:
string frequencySort(string s) {
unordered_map<char, int> mp;
string ans = "";
for (int i = 0; i < s.length(); ++i) {
mp[s[i]]++;
}
vector<pair<char, int>> v(mp.begin(), mp.end());
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); ++i) {
while (v[i].second--) {
ans += v[i].first;
}
}
return ans;
} private:
static bool cmp(pair<char, int> a, pair<char, int> b) {
return a.second > b.second;
}
};
451. Sort Characters By Frequency (sort map)的更多相关文章
- LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- 【leetcode】451. Sort Characters By Frequency
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...
- 451. Sort Characters By Frequency将单词中的字母按照从高频到低频的顺序输出
[抄题]: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: I ...
- [LeetCode] 451. Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LC] 451. Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- 451. Sort Characters By Frequency(桶排序)
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- 451. Sort Characters By Frequency
题目: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Inp ...
- #Leetcode# 451. Sort Characters By Frequency
https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...
- 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...
随机推荐
- jquery+easyui主界面布局一例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="workbench.aspx ...
- RestTemplate请求
JSONObject json = new JSONObject(sendParam);HttpHeaders headers = new HttpHeaders();MediaType type = ...
- esri和ArcGIS
1 esri esri是environment system research institute,环境系统研究所.总部在美国加州.它是世界上最大的GIS技术提供商. 主要产品有ArcGIS.ArcV ...
- Package template (html/template) ... Types HTML, JS, URL, and others from content.go can carry safe content that is exempted from escaping. ... (*Template) Funcs ..
https://godoc.org/text/template GoDoc Home About Go: text/templateIndex | Examples | Files | Directo ...
- Java NIO 粘包 拆包 (实战) - 史上最全解读
疯狂创客圈 Java 聊天程序[ 亿级流量]实战系列之13 [博客园 总入口 ] 本文的源码工程:Netty 粘包/半包原理与拆包实战 源码 本实例是<Netty 粘包/半包原理与拆包实战> ...
- Linux就该这么学--命令集合6(打包压缩文件、文件查询搜索命令)
1.tar命令用于对文件打包压缩或解压:(tar [选项] [文件]) 打包并压缩文件:tar -czvf 压缩包名.tar.gz 文件名 解压并展开压缩包:tar -xzvf 压缩包名.tar.gz ...
- 删除SVN账号
删除里面的所有文件 C:\Users\Administrator\AppData\Roaming\Subversion\auth
- SAP 系统账期开关
(1)OB52 财务账期-C 财务维护表 T001B[维护表T001B] (2)OB29 -C FI 财政年变式 (3)MMPV / MMRV -物料账期 MMPV 商品会计期间设置-结帐期间 [ 如 ...
- SAP建数据库索引
[转]SAP建数据库索引 %_hints db6 'INDEX("MKPF","MKPF~BUD")' db6 'INDEX(&quo ...
- performSelector: 与 dispatch_time 异同
iOS中timer相关的延时调用,常见的有NSObject中的performSelector:withObject:afterDelay:这个方法在调用的时候会设置当前runloop中timer,还有 ...