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的更多相关文章

  1. LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  2. [LeetCode] 451. Sort Characters By Frequency 根据字符出现频率排序

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  3. 【leetcode】451. Sort Characters By Frequency

    Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...

  4. 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...

  5. 451. Sort Characters By Frequency

    题目: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Inp ...

  6. 451. Sort Characters By Frequency将单词中的字母按照从高频到低频的顺序输出

    [抄题]: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: I ...

  7. 451. Sort Characters By Frequency (sort map)

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  8. [LC] 451. Sort Characters By Frequency

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  9. 451. Sort Characters By Frequency(桶排序)

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

随机推荐

  1. 05- views视图层

    1.配置index 主url from django.contrib import admin from django.urls import path, re_path, include urlpa ...

  2. 4 CRM-权限管理rbac、github代码

    1.引入权限组件rbac 1.settings配置app.中间件 INSTALLED_APPS = [ ... ... 'crm.apps.CrmConfig', "stark.apps.S ...

  3. 【Vijos】lxhgww的奇思妙想

    题面 题解 求$k$级祖先孙子 为什么要用长链剖分啊??? 倍增并没有慢多少... 其实是我不会 长链剖分做这道题还是看这位巨佬的吧. 代码 #include<bits/stdc++.h> ...

  4. Linux下的文件系统结构

    文章链接:https://blog.csdn.net/qq_38646470/article/details/80159630

  5. 并发系列(四)-----CAS

    一 简介 保证Java中的原子操做方式有两种方式  1 加锁(可以理解悲观锁机制)  2 CAS(可以理解为乐观锁机制)  CAS全称是Compare and Swap 即比较并替换.在JDK中许多地 ...

  6. JUC——线程同步辅助工具类(Semaphore,CountDownLatch,CyclicBarrier)

    锁的机制从整体的运行转态来讲核心就是:阻塞,解除阻塞,但是如果仅仅是这点功能,那么JUC并不能称为一个优秀的线程开发框架,然而是因为在juc里面提供了大量方便的同步工具辅助类. Semaphore信号 ...

  7. Go简单聊天

    用Go简单实现网络通信 其余功能可以在这个模型上继续加,比如增加通信人数,实现聊天 server 端 package main import ( "fmt" "log&q ...

  8. kubeadm 线上集群部署(一) 外部 ETCD 集群搭建

      IP Hostname   192.168.1.23 k8s-etcd-01 etcd集群节点,默认关于ETCD所有操作均在此节点上操作 192.168.1.24 k8s-etcd-02 etcd ...

  9. Windows 本地文件搜索神器

    Wox: Windows 本地文件搜索神器 下载地址: https://github.com/Wox-launcher/Wox 注: Wox只能搜索C盘下的文件,所以需要结合everything 如果 ...

  10. Django_分页

    目录 基本语法 示例 示例1 使用django内置Paginator模块 示例2 改写Paginator 示例3 自定义pager组件 示例3.1 objs与pager各自单独使用 示例3.2 obj ...