堆排序——LeetCode451. 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

思路

这道题的意思是给一个字符串,要求对字符串里的字符顺序进行调整,使得调整后字符串里的字符是根据其出现频率从高到低排列。因此首先遍历这个字符串,利用map获得每个字符出现的频率,然后再使用堆排序,最后根据堆排序的结果重新构建新的字符串。

代码


typedef struct node{char ch;int fre;
};class Solution {public:vector<node>number;void maximun(int i, int heap_size) {int l, r, m;node temp;l = 2*i; r = 2*i+1; m = i;temp = number[i];if(l<=heap_size && number[l-1].fre>number[i-1].fre) {m = l;temp = number[l-1];}if(r<=heap_size && number[r-1].fre>number[m-1].fre) {m = r;temp = number[r-1];}if(m!=i) {number[m-1] = number[i-1];number[i-1] = temp;maximun(m, heap_size);}}void heap_build() {int i, j;for(i = number.size()/2; i>0; i--) {maximun(i, number.size());}}void heap_sort() {heap_build();int len, i, j;node temp;len = number.size();while(len>1) {temp = number[0];number[0] = number[len-1];number[len-1] = temp;maximun(1, len-1);len--;}}string frequencySort(string s) {int i, j;string res = "";map<char, int> m;map<char, int>::iterator iter;m.clear();for(i=0; i<s.length(); i++) {if(m.count(s[i])==0) {m[s[i]] = 1;}else {m[s[i]]++;}}number.clear();iter = m.begin();while(iter!=m.end()) {node t;t.ch = iter->first;t.fre = iter->second;number.push_back(t);iter++;}heap_sort();stringstream str;for(i=number.size()-1; i>=0; i--) {j = number[i].fre;while(j--) {res += number[i].ch;// res.append(number[i].ch.tostring());}}return res;}
};

堆排序——LeetCode451. Sort Characters By Frequency相关推荐

  1. 451 Sort Characters By Frequency

    451. Sort Characters By Frequency 先统计次数,再按次数构造新的string.思路没什么难,写起来还有点麻烦.用了priorityqueue.. class Wrapp ...

  2. leetcode: 451. Sort Characters By Frequency

    451. Sort Characters By Frequency Given a string, sort it in decreasing order based on the frequency ...

  3. LeetCode 451. Sort Characters By Frequency

    用hash表记录每个字符的个数,开始我是新建了一个vector<pair<int,char>>然后依据个数排序. 后来发现可以直接对string排序. 其中运用了lambda表 ...

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

    题目描述: 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree"输出: "eert"解释: 'e'出现两次,'r'和't ...

  5. LeetCode Sort Characters By Frequency

    题意:给出一个字符串s,按字符出现频率排序 代码如下: class Solution(object):def frequencySort(self, s):""":typ ...

  6. Python入门篇-数据结构堆排序Heap Sort

    Python入门篇-数据结构堆排序Heap Sort 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.堆Heap 堆是一个完全二叉树每个非叶子结点都要大于或者等于其左右孩子结点的 ...

  7. C语言堆排序Heap Sort算法(附完整源码)

    堆排序Heap Sort算法 堆排序Heap Sort算法的完整源码(定义,实现,main函数测试) 堆排序Heap Sort算法的完整源码(定义,实现,main函数测试) #include < ...

  8. 经典排序算法 - 堆排序Heap sort

    经典排序算法 - 堆排序Heap sort 堆排序有点小复杂,分成三块 第一块,什么是堆,什么是最大堆 第二块,怎么将堆调整为最大堆,这部分是重点 第三块,堆排序介绍 第一块,什么是堆,什么是最大堆 ...

  9. 堆排序 Heap Sort

最新文章

  1. Docker 用法总结之:管理工具 shipyard 的具体使用指南
  2. 贪心 ---- 2020牛客多校第3场[Clam and Fish+贪心]
  3. Android 第三方库RxLifecycle使用
  4. PHP制作订货,PHP生成订单号的两种方法
  5. java程序在centos7里面开机自启动
  6. ORACLE数据库的连接
  7. VS修改生成应用图标
  8. 数据库——Oracle(1)
  9. Nginx总复习---1
  10. 多项式拟合怎么确定次数_PyTorch入门4 搭建多项式回归模型
  11. Java并行编程–从并行任务集获取反馈
  12. LeetCode 141. 环形链表(Linked List Cycle) 19
  13. android 集成魅族推送,魅族推送通道集成指南
  14. 阿里云CentOS服务器搭建静态网站(零基础)
  15. C# 导入.reg文件
  16. K650c + Ubuntu 15.04无法正常关机,重启
  17. 化学反应的常微分方程求解(Python)
  18. OFDM 符号的概念
  19. req和res的作用
  20. win10内网穿透实现远程桌面连接

热门文章

  1. Android M Developer Preview - API Preview(一)
  2. 防飞溅装置(挡泥板)emark认证详解
  3. 下列关于python的说法中_下列选项中,关于Python说法错误的是()
  4. ESP32 AT指令连接AWS亚马逊云
  5. python初学者:输入实数(可能有整数和带小数的浮点数),升序后输出
  6. 英寸和厘米转化python_习题 5: 更多的变量和打印 | 笨办法学 Python
  7. Unity 播放全景视屏
  8. c语言经典程序情书,计算机专业情书.doc
  9. java情书_java情书《面向对象》
  10. 客观评价 增长趋势比 vite 还猛的 TailwindCSS