当前位置:网站首页>Buckle Exercise - 61 Sort by frequency of characters
Buckle Exercise - 61 Sort by frequency of characters
2022-08-10 11:34:00 【qq_43403657】
61 根据字符出现频率排序
1.问题描述
给定一个字符串,请将字符串里的字符按照出现的频率降序排列,如果频率相同,则按照字符的ASCII码升序排列.
示例 1:
输入:
“tree”
输出:
“eert”
解释:
'e’出现两次,'r’和’t’都只出现一次.
因此’e’必须出现在’r’和’t’之前,而且’r’比’t’的ASCII码小.
示例 2:
输入:
“cccaaa”
输出:
“aaaccc”
解释:
'c’和’a’都出现三次.So in ascending order of characters,'a’在’c’前.
示例 3:
输入:
“Aabb”
输出:
“bbAa”
解释:
'A’和’a’被认为是两种不同的字符,并且’A’的ASCII码比’a’小
2.输入说明
输入一个字符串
3.输出说明
输出一个字符串,Please refer to the above description for the order of characters in the string.
4.范例
输入
Aabb
输出
bbAa
5.代码
#include<iostream>
#include<map>
#include<string>
#include<unordered_map>
#include<algorithm>
using namespace std;
bool cmp(pair<char, int> a, pair<char, int>b)
{
return a.second == b.second?a.first<b.first:a.second>b.second;//该题目中最关键的部分,注意写法!
}
string OrderbyChar(string s)
{
//给定一个字符串,请将字符串里的字符按照出现的频率降序排列,如果频率相同,则按照字符的ASCII码升序排列.
string res;//结果字符串
unordered_map<char, int>hash;//记录每个字符出现次数
vector<pair<char, int> > v;//使用pair记录对应关系
for (auto c : s)//遍历字符串
hash[c]++;
for (auto it : hash)
v.emplace_back(it);//直接将每个hash元素插入v中
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); i++)
{
while (v[i].second--)
res += v[i].first;
}
return res;
}
int main()
{
string s;
cin >> s;
string res = OrderbyChar(s);
cout << res << endl;
return 0;
}
边栏推荐
- POJ 1026 Cipher (Permutation Groups)
- A little self-deprecating deconstruction about farmers "code"
- 使用哈工大LTP测试分词并且增加自定义字典
- leetcode 823. Binary Trees With Factors(因子二叉树)
- Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
- 什么是幂等性?四种接口幂等性方案详解!
- 力扣练习——61 根据字符出现频率排序
- JWT implements login authentication + Token automatic renewal scheme
- Short video software development - how to break the platform homogenization
- What is an abstract class
猜你喜欢

Store limited time seckill function system

Emulate stm32 directly with proteus - the programmer can be completely discarded

mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded

为什么Redis很快

Module 9 - Designing an e-commerce seckill system

第5章相似矩阵及二次型(4)

3 injured in 'electrical accident' at Google data center

MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细

从产品维度来看 我们为什么不能完全信任Layer2?

中小规模网站架构
随机推荐
Double.doubleToLongBits()方法使用
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
即时零售业态下如何实现自动做账?
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded
力扣练习——61 根据字符出现频率排序
阻塞 非阻塞 poll机制 异步
使用.NET简单实现一个Redis的高性能克隆版(六)
程序员追求技术夯实基础学习路线建议
英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
力扣练习——64 最长和谐子序列
Three-phase 380V rectified voltage
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
LeetCode_152_乘积最大子数组
Emulate stm32 directly with proteus - the programmer can be completely discarded
实现内网穿透的最佳解决方案(无实名认证,完全免费)
模块九 - 设计电商秒杀系统
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
力扣练习——63 找到字符串中所有字母异位词
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了