Leetcode 451. Sort Characters By Frequency (用堆)

数字设计与计算机体系结构习题答案,数字设计和计算机体系结构习题答案DigitalDesignandComputerArchitecture.pdf... 数字设计和计算机体系结构习题答案DigitalDesignandComputerArchitecture.pdfDavid Money Harris and Sarah L. Harris, Digital Design and Computer Architecture, © 2007 by Elsevier Inc.Exercise SolutionsC H A P T E R ... 阅读详情
  1. Sort Characters By Frequency
    Medium

Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.

Return the sorted string. If there are multiple answers, return any of them.

Example 1:

Input: s = “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: s = “cccaaa”
Output: “aaaccc”
Explanation: Both ‘c’ and ‘a’ appear three times, so both “cccaaa” and “aaaccc” are valid answers.
Note that “cacaca” is incorrect, as the same characters must be together.
Example 3:

Input: s = “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.

Constraints:

1 <= s.length <= 5 * 105
s consists of uppercase and lowercase English letters and digits.

解法1:

class Solution {
public:
    string frequencySort(string s) {
        priority_queue<pair<int, char>> maxHeap;
        unordered_map<char, int> mp;
        for (auto c : s) mp[c]++;
        for (auto m : mp) {
            maxHeap.push({m.second, m.first});
        }
        string res = "";
        while (!maxHeap.empty()) {
            auto top = maxHeap.top();
            maxHeap.pop();
            for (int i = 0; i < top.first; i++) res += top.second;   //注意不要写成res = res + top.second; 不然内存不够
        }
        return res;
    }
};

解法2:

struct Node {
    char ch;
    int freq;
    Node(char c, int f) : ch(c), freq(f) {}
    bool operator < (const Node & n) const {
        return freq < n.freq;
    }
};

class Solution {
public:
    string frequencySort(string s) {
        int n = s.size();
        priority_queue<Node> maxHeap;
        unordered_map<char, int> char2Freq;
        for (auto c : s) {
            char2Freq[c]++;
        }
        for (auto cf : char2Freq) {
            maxHeap.push(Node(cf.first, cf.second));
        }
        string res = "";
        while (!maxHeap.empty()) {
            Node top = maxHeap.top();
            maxHeap.pop();
            for (int i = 0; i < top.freq; i++) res += top.ch; //注意不要写成res = res + top.second; 不然内存不够
        }
        return res;
    }
};
【零基础入门unity游戏开发——3D篇】3D物理关节 —— Joint相关组件 阅读详情

相关推荐

使用vite+react+ts+Ant Design开发后台管理项目(四)

本文将引导开发者从零基础开始,运用、react、react-router、react-redux、Ant Design、less、tailwindcss、axios等前沿技术栈,构建一个高效、响应式的后台管理系统。通过详细的步骤和实践指导,文章旨在为开发者揭示如何利用这些技术工具,从项目构思到最终实现的全过程,提供清晰的开发思路和实用的技术应用技巧。使用vite+react+ts+Ant Design开发后台管理项目(一)使用vite+react+ts+Ant Design开发后台管理项目(二)

lbking666666的专栏 2020

Sort Characters By Frequency

根据字符出现频率重新排序字符串。 先用一个大小为256的数组counter计算存储每个字符出现的次数。 再用类型为vector string frequencySort(string s) { int counter[256] = {0}; int len = s.length(); for(int i=0;i<len;++i)

罗布泊coding 347

S32K1XX调试--快速定位HardFault

1、背景 程序运行,发现程序跑飞到HardFault,但不清楚为什么会跑到HardFault中断处理函数去。 2、分析 要想知道为什么会跑到HardFault_Handler中去,就很有必要了解一下,程序上一条执行的是那条命令。 根据汇编指令可以定位上条指令,但如果对汇编指令不是很了解的话,那就很头疼了 根据单步运行也能实现定位,但如果程序量很大,又很杂,可能花费的时间就比较久些。 3、问题查找 提供一种不需要懂汇编指令的查找方法,方法如下: 在中断处理函数中插入以下代码,以及另外定义

ping_devil的博客 4029

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 '

分享人工智能学习心得与实践经验,探讨应用场景,见证变革与进步 599

5.4 磁悬浮轴承控制系统仿真:在MATLAB/Simulink中搭建包含功放、传感器模型的闭环系统模型,进行稳定性与动态性能仿真

磁悬浮轴承控制系统仿真方法 摘要:磁悬浮轴承控制系统仿真需建立包含转子动力学、传感器、控制器、功率放大器和电磁执行器的高保真非线性模型。通过MATLAB/Simulink搭建闭环系统,重点建模传感器带宽限制与噪声、功放饱和特性以及电磁力非线性。仿真分析包括时域阶跃响应、扰动抑制测试,频域稳定性分析(伯德图、奈奎斯特图),以及参数摄动下的鲁棒性验证。关键要验证控制算法在非线性因素影响下的稳定性和动态性能,为实际系统实现提供可靠依据。仿真中需特别注意物理量单位一致性和数字控制离散化效应。

技术工作者 1119

RK3588平台-ES8388音频驱动调试与设备树配置实战

本文详细解析了在RK3588平台上调试ES8388音频驱动的实战过程。内容涵盖Linux音频驱动的三层架构(Machine、Platform、Codec),并重点提供了设备树(DTS)的完整配置示例,包括I2C节点、声卡节点及音频路由设置。文章还分享了驱动加载验证、amixer调试命令以及常见问题(如无声、变调)的排查思路,旨在帮助开发者快速完成RK3588与ES8388的音频系统集成。

mango的博客 818

[LeetCode] Sort Characters By Frequency(Java)

我的思想是list数组按顺序构造,讨论中高票使用数组实现,效果更好 我的代码 public class Solution { public String frequencySort(String s) { int length = s.length(); List[] sortList = new LinkedList[length+1];

jsh0123的博客 1067

软件测试--用例篇(测试用例的好处、测试用例的七种设计方法、测试用例的粒度、测试用例的评价)

作为软件测试工程师,最主要的工作就是:编写测试用例,那么为什么要编写测试用例?它有什么好处?如何编写测试用例?测试用例写简单好还是复杂好?如何评价测试用例的质量? 天呐撸!能说不会吗?! 作为倾向软件测试工程师的老铁们,还是一起来学习吧! 一、编写测试用例的好处 测试用例是测试者的依据 测试用例使得工作可重复,是自动化测试的基础 补充:自动化测试的前提条件是:功能比较稳定,页面...

cherrydreamsover的博客 3741

[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" Ex

小榕流光的专栏 4390

海康视觉脚本开发全流程解析

本文详细解析了海康机器视觉平台(VM)中脚本模块的开发全流程。从创建第一个脚本模块入手,深入讲解了输入参数、输出参数与内部变量的定义与区别,并提供了C#脚本编写、公用函数调用、代码结构组织以及编译调试的实用指南。旨在帮助开发者掌握海康视觉脚本开发的核心技巧,高效实现复杂的自定义视觉逻辑处理。

beer8的博客 613

学 Simulink—— 基于强化学习(DDPG)的电机无位置传感器控制仿真

Actor:输入状态s,输出θ̂_dot(即 ω̂),积分得 θ̂Critic:评估"估计角度与反电动势模型一致性"的 Q 值奖励函数不依赖真实 θ(无监督式自监督),只用iαβ预测残差。

amy_mhd的博客 22

LeetCode //C - 451. Sort Characters By Frequency

【代码】LeetCode //C - 451. Sort Characters By Frequency。

Made in Code 1536

微信小程序 - 真机调试请求异常 Provisional headers are shown,微信小程序开发者工具调试没问题就真机测试时出现请求报错(微信小程序请求接口提示Provisional h)

微信小程序开发 Request Headers: Provisional headers are shown,微信开发工具请求正常,真机调试和上线之后出现 Provisional headers are shown,微信小程序开发工具调试没问题,真机调试Provisional headers are shown,在开发工具调试,请求正常没问题,使用真机调试,请求异常,微信小程序真机调试时后台是本地服务器,显示“Provisional headers are shown”问题解决,微信小程序websocket

🏆 前端领域优质创作者 1万+

【Leetcode】451. 根据出现的频率对字符排序(Sort Characters By Frequency)

Leetcode - 451 Sort Characters By Frequency (Medium) Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. There...

厉兵秣码 244

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

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" Ex

James Pan 1246

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode - 451. Sort Characters By Frequency(按照字符出现次数排序(简单题))

LeetCode - 451. Sort Characters By Frequency(按照字符出现次数排序(简单题)) 题目链接 题目 解析 很简单的题目,直接按照出现次数排序即可,这里只是为了熟练Java8的写法以及代码的优化。上面代码是自己写的(比较冗余),下面的是参考别人的。 class Solution { private class Pair implements...

zxzxin的博客~ 266

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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: “tree” Output: “eert” Explanation: ‘e’ appears twice whil...

fengran_mark的博客 284

LeetCode-451. Sort Characters By Frequency [C++][Java]

Given a strings, sort it indecreasing orderbased on thefrequencyof the characters. Thefrequencyof a character is the number of times it appears in the string. Returnthe sorted string. If there are multiple answers, returnany of them.

贫道绝缘子的博客 701

LeetCode OJ 451. Sort Characters By Frequency

LeetCode OJ 451. Sort Characters By FrequencyDescriptionGiven a string, sort it in decreasing order based on the frequency of characters.Example1 Input: “tree” Output: “eert” Explanat

jan_deng的博客 507

[LeetCode]451. Sort Characters By Frequency

Given a string, sort it in decreasing order based on the frequency of characters. 点击打开链接 这是一道运用HashMap的相关基础用法的题。 思路: 1:统计每个字母和出现相应次数; 2:把字母和出现次数的相应关系储存到list里; 3:按出现次数把第二步list里的相应关系按升序排列;

offerkkl的博客 1045

LeetCode笔记:451. Sort Characters By Frequency

将字符串中字符按照出现次数排序重组

Minecraft 1610

[Leetcode 451] Sort Characters By Frequency

451. Sort Characters By Frequency 451. 根据字符出现频率排序 Intuition Count all the characters and rebuild the output string by sorting. Complexity Time: O(nlogn), where n is the length of input string. Space...

zdxiq000的专栏 411

leetcode 451 Sort Characters By Frequency C++

这道题我先记录每个字母出现的频率,然后做了一个降序,然后依次赋值给字符串就行了。 static bool compare(paira,pairb) { return a.second>b.second; } string frequencySort(string s) { unordered_map hashMap; vec

wcxdell的专栏 1235

Leetcode 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.

wshish920907的博客 408

leetcode 451. Sort Characters By Frequency 根据字符出现频率排序

用桶排序来解决这个问题。使用hashmap来统计字符出现的频率,统计完再构建一个以频率为key的顺序队列,然后再出队列,出队列的时候把字符按频数追加到字符串返回值上。

ln_ydc的专栏 193

leetcode 451. Sort Characters By Frequency

目录 一、问题描述 二、代码实现 1、堆 2、桶排序 https://leetcode.com/problems/sort-characters-by-frequency/ 基于出现频率对字符串进行降序排序后返回。 一、问题描述 测试用例: Example 1: Input: "tree" Output: "eert" Explanation: 'e' appear...

小景的博客 241
上一篇: Leetcode 313: Super Ugly Number (超级丑数)
下一篇: Leetcode1845. Seat Reservation Manager (堆的应用)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值