451. Sort Characters By Frequency
来源:互联网 发布:极课大数据员工离职 编辑:程序博客网 时间:2024/06/01 21:12
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.
Seen this question in a real interview before?
class Solution { public String frequencySort(String s) { Map<Character, Integer> data = new HashMap<>();for (int i = 0; i < s.length(); ++i) {char c = s.charAt(i);data.put(c, data.getOrDefault(c, 0) + 1);}char[] heap = new char[data.size() + 1];int N = 0;for (Character c : data.keySet()) {heap[++N] = c;swim(heap, data, N);}StringBuilder re = new StringBuilder();for (int i = 0; i < data.size(); ++i) {char c = heap[1];int n = data.get(c);for(int j = 0; j < n; ++j)re.append(c);exch(heap, 1, N--);//heap[N + 1] = null;sink(heap, data, 1, N);}return re.toString(); } public void swim(char[] heap, Map<Character, Integer> data, int n) {while (n > 1 && bigger(n, n / 2, data, heap)) {exch(heap, n, n / 2);n /= 2;}}public void sink(char[] heap, Map<Character, Integer> data, int i, int N) {while (i * 2 <= N) {int j = i * 2;if (j < N && bigger(j + 1, j, data, heap))j += 1;if (bigger(i, j, data, heap))break;exch(heap, i, j);i = j;}}public void exch(char[] heap, int i, int j) {char c = heap[i];heap[i] = heap[j];heap[j] = c;return;}public boolean bigger(int i, int j, Map<Character, Integer> data, char[] heap) {return data.get(heap[i]) > data.get(heap[j]);}}
阅读全文
0 0
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency**
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- 451. Sort Characters By Frequency
- hdfs dfsadmin -reconfig reload datanode dfs.datanode.data.dir without restart datanode
- 1000以内的所以完数
- HTML5 单个或者多个Video标签视频加载第一帧方法(poster属性)
- FastDFS分布式文件系统安装与使用(单节点)
- LeetCode:67. Add Binary
- 451. Sort Characters By Frequency
- ireport中一页显示多少条记录的问题
- linux中cron定时任务详解---------文中部分命令适用于redhat6和Centos6
- 解决java.lang.IndexOutOfBoundsException: Index: 0, Size: 0的问题
- 消息提示插件:toastr.js
- 所有的3位数对称素数
- 一个关于LigerUI打开一个弹窗页面并将数据返回的方法
- tag()标签的正确传值方式
- [JAVA] JDBC简述