【LeetCode】Hash
[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的更多相关文章
- 【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 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- python实现WordCount(第三次作业)
0x00 注明 合作者:201631062315 201631062310 代码地址:https://gitee.com/c1e4r/word-count2 作业地址:https://edu.cnbl ...
- 三、Redis的配置文件和多数据库用途
1.使用文件 # 使用配置文件启动 redis-server ./redis.conf # 带配置文件启动 且指定某几个配置 配置名称前加 -- redis-server ./redis.conf - ...
- find和grep技巧
1. find ./ -name "*streaming*" 查找文件 2. grep -r KUBE_LOGTOSTDERR /etc/kubernetes/* 查找内容
- 使用 v-html 绑定值
<div id="app03"> <div v-html="message"></div> <!--这里使用v-htm ...
- loadRunner函数之web_find
int web_find( const char *StepName, <Attributes and Specifications list>, char *searchstring, ...
- 07-图5 Saving James Bond - Hard Version(30 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 低价购买 (动态规划,变种最长下降子序列(LIS))
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- 在Delphi中使用系统对应文件类型的图标
在应用程序的编写中,组合框(ComboBox).列表框(ListBox).等常见的部件,通常不仅要用于显示文字,而且还要显示其与文字相关的图标.在一般的Windows应用程序中,这些图标的显示都要随列 ...
- win7 SP1 64位 原版 百度网盘下载
下载地址:https://pan.baidu.com/s/1bnOtKU5YH4gSr1RmZR2BkQ 提取码 :s9o7 扫码下载:
- [CSP-S模拟测试47]反思+题解
打开题面,T3似乎被换过了.(那我就更有理由直接弃掉了) T1是我最害怕的乱搞题,赶紧扔了看T2.发现是个sb板子?雨天的尾巴弱化版? 然而线段树合并早忘干净了(最近几道可以线段树合并的题都是用别的方 ...