[451] Sort Characters By Frequency [Medium]

给一个字符串,要求返回按照字母出现频率的排序后的字符串。(哈希表+桶排)

有个技巧是Hash用Value作为Index放到桶里。

 class Solution {
public:
string frequencySort(string s) {
map<char, int> mp;
for (auto c : s) {
mp[c]++;
}
string ans = "";
// 桶排序
vector<string> bucket(s.size()+, "");
for (auto ele : mp) {
char ch = ele.first;
int cnt = ele.second;
bucket[cnt].append(cnt, ch);
}
for (auto i = bucket.size() - ; i >= ; --i) {
ans += bucket[i];
}
return ans;
}
};

【LeetCode】Hash的更多相关文章

  1. 【leetcode】Find All Anagrams in a String

    [leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...

  2. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  3. 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)

    [LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. 2019-8-31-Latex-公式速查

    title author date CreateTime categories Latex 公式速查 lindexi 2019-08-31 16:55:58 +0800 2018-05-25 16:5 ...

  2. 虚拟机复制的linux无法联网,解决Bringing up interface eth0: Device eth0 does not seem to be present, delaying initialization.

    一.问题描述 OS:centos 原因是拷贝虚拟机造成的. 使用vmworkstation打开虚拟机的时候,要选择copy而非move. 二.解决描述 网络上解决步骤各异,其实就一句话.只要保证vmw ...

  3. cnblogs添加打赏

    上图上真相 1.进入后台设置---文件 2.上传你的支付宝和微信收款码(注意图片格式为bmp格式) 2.还是上图的位置,选择设置选项,找到博客侧边栏公告(支持HTML代码)(支持JS代码) 3.将如下 ...

  4. pod的状态分析

    Pod状态 状态 描述 Running 该 Pod 已经绑定到了一个节点上,Pod 中所有的容器都已被创建.至少有一个容器正在运行,或者正处于启动或重启状态. Pending Pod 已被 Kuber ...

  5. js对象的深度拷贝

    //判断对象的类型 Array Object Function String Number ..... function getObjType(obj){ return Object.prototyp ...

  6. makefile 中的patsubst

    1. wildcard:扩展通配符 2. notdir:去除路径 3. patsubst:替换通配符 若有一个makefile如下: src=$(wildcard *.c ./sub/*.c) dir ...

  7. PHP getcwd() 函数

    获取当前工作目录: <?phpecho getcwd()?> 结果: /home/php 定义和用法 getchwd() 函数返回当前工作目录. 语法 getcwd(); 技术细节 返回值 ...

  8. hdu 2089 不要62 (数位dp)

    Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别 ...

  9. winform 控件拖拽和缩放

    核心类: using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using ...

  10. 外部表及oracle数据库内存

    create table alert1 (log varchar2(1000))2 organization external3 (type oracle_loader4 default direct ...