#Leetcode# 451. Sort Characters By Frequency
https://leetcode.com/problems/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 = "";
int len = s.length();
unordered_map<char, int> mp;
priority_queue<pair<int, char> > q;
for(int i = 0; i < len; i ++)
mp[s[i]] ++;
for(auto i : mp)
q.push({i.second, i.first});
while(!q.empty()) {
int k = q.top().first;
char c = q.top().second;
for(int i = 0; i < k; i ++)
ans += c;
q.pop();
}
return ans;
}
};
优先队列
明天就肥家放假啦
#Leetcode# 451. Sort Characters By Frequency的更多相关文章
- 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, 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 ...
- 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...
- 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 ...
- 451. Sort Characters By Frequency (sort map)
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: ...
随机推荐
- PHP 练习(投票)
1.建立数据库 表1:DiaoYanTiMu 表2:DiaoYanXuanXiang 2.页面 页面1:投票首页 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- 【转载】COM 组件设计与应用(六)——用 ATL 写第一个组件
原文:http://vckbase.com/index.php/wv/1216.html 一.前言 1.与 <COM 组件设计与应用(五)>的内容基本一致.但本回讲解的是在 vc.net ...
- 【转载】COM 组件设计与应用(四)——简单调用组件
原文:http://vckbase.com/index.php/wv/1211.html 一.前言 同志们.朋友们.各位领导,大家好. VCKBASE 不得了, 网友众多文章好. 组件设计怎么学? 知 ...
- 洛咕 P2468 [SDOI2010]粟粟的书架
强行二合一啊... 前面直接二分最小值,二维前缀和.后面用主席树查最小值.注意要写\(nlogn\). // luogu-judger-enable-o2 #include<bits/stdc+ ...
- sqlyog 可视化查看数据库关系并创建外键
+ 一个架构设计器,把表拖进来即可查看数据库关系 如果要建立外键,需要在两个要被建立的外键之间进行操作(经过验证不需要都为主键),然后从用鼠标把子外键拖到父外键中,就可以关联上了. 参考: https ...
- Linux 挂载 xshell 命令 配置环境变量
- 微信小程序如何检测接收iBeacon信号
前话 微信小程序开发带着许多坑,最近就遇到了个需求,检测iBeacon来进行地点签到. (╯▔皿▔)╯ 微信小程序对于iBeacon的文档也写的十分精简,只简单介绍了每个接口的作用,这就导致我以为简单 ...
- $(document)和$(window)各是什么意思?
jquery中的对象$(document) 是当前文档,就是你看到的整个网页$(window) 如果没有框架则就是你浏览的当前浏览器的窗口 将document, window转换为jquery对象 比 ...
- 3、ObjectARX开发创建直线、圆、圆弧和修改对象属性
一.本节课程 Arx二次开发创建直线.圆.圆弧和修改对象属性 二.本节要讲解的知识点 1.如何应用C++ ARX二次开发创建直线. 2.如何应用C++ ARX二次开发创建圆. 3.如何应用C++ AR ...
- C语言学习之路之基础变量
Hello,大家好,今天又和大家见面了!前两天,我看到了几款游戏引擎渲染效果的对比的视频,https://www.bilibili.com/video/av5113296?from=search&am ...