[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. 【记录】vue相关知识点

    let let是es6新引入的命令,与var命令类似,但是let是声明的局部变量,只在所在代码块中有效. ES5 只有全局作用域和函数作用域,没有块级作用域,这带来很多不合理的场景. var s = ...

  2. vue 如何读取编译携带的参数

    vue 环境有很多套,我们需要根据不同环境设置不同的一些参数,如何不装任何依赖的情况下获取参数 下面是我制作官网,需要根据开发还是生产环境配置不同CDN,用vue-cli2+webpack,配置是再: ...

  3. JSON和fastjson

    一.JSON 语法规则: 数据在名称/值对中 数据由逗号分隔 花括号保存对象 方括号保存数组 名称/值对-->对象-->数组 JSON 名称/值对 JSON 数据的书写格式是:名称/值对. ...

  4. 【leetcode】552. Student Attendance Record II

    题目如下: Given a positive integer n, return the number of all possible attendance records with length n ...

  5. 开源实践分享:Ceph bluestore部署实践

    https://blog.51cto.com/99cloud/2119884 Ceph bluestore部署 首先为大家分享Ceph bluestore具体该如何部署,使用环境如下• 单节点• Ce ...

  6. src/lib/framework/src/driverFramework.cpp学习

    int Framework::initialize() { DF_LOG_DEBUG("Framework::initialize"); g_framework = new Syn ...

  7. 10个优秀的 Web UI库/框架

    UI(User Interface)即用户界面,也称人机界面.是指用户和某些系统进行交互方法的集合,实现信息的内部形式与人类可以接受形式之间的转换.本文为WUI用户整理了10个优秀的 Web UI 库 ...

  8. ios和android适配

    一些情况下对非可点击元素如(label,span)监听click事件,ios下不会触发 解决方案:css增加cursor:pointer; 三星手机遮罩层下的input.select.a等元素可以被点 ...

  9. 如何把EXCEL数据导入到SQL SERVER数据库中 (转)

    转:http://blog.csdn.net/jjp837661103/article/details/13509889 在我们完成一个项目开发之后,通常我们需要把客户的很多数据导入到数据库中,面对大 ...

  10. eclipse run error:g++ not found in Path

    网上有人说: 在eclipse下 windows-->Preference-->C/C++-->Build-->Setting然后选择Discovery标签,将里面的内容全部R ...