Heap-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.
- class Solution {
- public:
- string frequencySort(string s) {
- string ans;
- vector<pair<char,int>> h;
- map<char,int> f;
- for(int i=;i<s.size();i++)
- {
- f[s[i]]++;
- }
- for(auto it:f)
- {
- pair<char,int> p(it.first,it.second);
- h.push_back(p);
- }
- sort(h.begin(),h.end(),[](pair<char,int> a,pair<char,int> b){
- return a.second>b.second;
- });
- for(int i=;i<h.size();i++)
- {
- string t(h[i].second,h[i].first);
- ans+=t;
- }
- return ans;
- }
- };
Heap-451. Sort Characters By Frequency的更多相关文章
- 【leetcode】451. Sort Characters By Frequency
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...
- LeetCode 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 ...
- 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
https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...
- 451. Sort Characters By Frequency (sort map)
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, 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: ...
- 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...
随机推荐
- 国内maven仓库地址
Maven 中央仓库地址: 1.http://mvnrepository.com/ (推荐) 2.http://mirrors.ibiblio.org/maven2/ 3.http://repo1.m ...
- csv中文乱码
处理办法:https://jingyan.baidu.com/album/3c48dd3464b46ce10be3581f.html?picindex=2
- linux小白
1. linux下加域名. 文件是在/etc/hosts 中间加的tab键 192.168.0.1 baidu.com linux下测试网页可以用 wget www.baidu.com 这个命 ...
- Hibernate不能实时获取MySQL数据库的更新
在hibernate.cfg.xml配置文件中,增加以下内容: <property name="hibernate.connection.provider_class"> ...
- IE浏览器调用jquery需要注意的小问题
今天在进行前端重构的时候发现了一个非常奇怪的浏览器兼容性问题,我想在网页上放一个JS的特效,于是下载了jquery-easyui,经过修改完成所需要的效果后,准备放入项目中,发现在IE浏览器中无法运行 ...
- 构建ASP.NET网站十大必备工具
最近使用ASP.NET为公司构建了一个简单的公共网站(该网站的地址:http://superexpert.com/).在这个过程中,我们使用了数量很多的免费工具,如果把构建ASP.NET网站的必备工具 ...
- 20155233 2016-2017-2 《Java程序设计》第8周学习总结
20155233 2016-2017-2 <Java程序设计>第8周学习总结 学习目标 了解NIO 会使用Channel.Buffer与NIO2 会使用日志API.国际化 会使用正则表达式 ...
- 实现KbmMw web server 支持https
在以前的文章里面介绍过kbmmw 做web server. 前几天红鱼儿非要我给他做一个支持https 的web server. 其实kbmmw 支持https 有好几种方法: 1. 使用isapi ...
- 2018.09.23 atcoder Boxes and Candies(贪心)
传送门 一道挺有意思的贪心. 从1到n依次满足条件. 注意要特判第一个数已经大于x的情况. 但是如何贪心吃呢? 如果靠左的数没有越界,我们吃靠右的数. 原因是下一次靠右的数就会成为靠左的数,相当于多贡 ...
- Django介绍(3)
https://www.cnblogs.com/yuanchenqi/articles/5786089.html