自定义Map.Entry的Comperator实现字符频率降序排序
leetCode (#451,middle) java实现
class Solution {
public String frequencySort(String s) {
Map<Character,Integer> map = new HashMap<>();
//放入map
for(char c :s.toCharArray()){
if(map.containsKey(c)){
map.put(c,map.get(c)+1);
}else
map.put(c,1);
}
//根据value的大小排序
Comparator<Map.Entry<Character,Integer>> comparator =
new Comparator<Map.Entry<Character, Integer>>() {
@Override
public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
return o2.getValue() - o1.getValue();
}
};
//放入链表
List<Map.Entry<Character,Integer>> list = new ArrayList<>(map.entrySet());
//根据实现的comp排序
Collections.sort(list,comparator);
//定义数组
StringBuilder sb = new StringBuilder();
for(Map.Entry<Character,Integer> entry : list){
for(int i = 0 ; i < entry.getValue();i++){
sb.append(entry.getKey());
}
}
return sb.toString();
}
}
自定义Map.Entry的Comperator实现字符频率降序排序的更多相关文章
- 用shell处理以下内容 1、按单词出现频率降序排序! 2、按字母出现频率降序排序! the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support
此题目有多种解法,sed.awk.tr等等,都可以解决此题,命令运用灵活多变. 编写shell脚本no_20.sh 解法1: #!/bin/bash ###-------------CopyRight ...
- shell脚本,按字母出现频率降序排序。
[root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...
- shell脚本,按单词出现频率降序排序。
[root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...
- java算法面试题:有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数 按值的降序排序,如果值相同则按键值的字母顺序
package com.swift; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; publi ...
- 集合(一)-Java中Arrays.sort()自定义数组的升序和降序排序
默认升序 package peng; import java.util.Arrays; public class Testexample { public static void main(Stri ...
- C语言:对长度为7的字符串,除首尾字符外,将其余5个字符按ASCII降序排序。-计算并输出3~n之间所有素数的平方根之和。
//对长度为7的字符串,除首尾字符外,将其余5个字符按ASCII降序排序. #include <stdio.h> #include <ctype.h> #include < ...
- Java 将Map按Value值降序排列
1 /** 2 * 将集合按照降序排列-FLOAT 3 * @param nowPartTwoData 4 * @return 5 */ 6 private static List<Map.En ...
- Map接口,Map.Entry,hashMap类,TreeMap类,WeakHashMap。
Collection接口之前接触过,每次保存的对象是一个对象,但是在map中保存的是一对对象,是以key->value形式保存的. 定义: public interface Map<K,V ...
- 10 HashMap,Map.Entry,LinkedHashMap,TreeMap,Hashtable,Collections类
Map集合的功能概述 添加功能 * V put(K key,V value):添加元素. * 如果键是第一次存储,就直接存储元素,返回null * 如果键不 ...
随机推荐
- mysql服务器没有响应
第一步删除c:\windowns下面的my.ini(可以先改成其它的名字也行) 第二步打开对应安装目录下\mysql\bin\winmysqladmin.exe 输入用户名 和密码(也可以忽略此步) ...
- Nginx的核心功能及应用实战
反向代理功能及配置: 反向代理(Reverse Proxy)方式是指以代理服务器来接受 internet 上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给interne ...
- 单例、异常、eval函数
一.单例 01. 单例设计模式 设计模式 设计模式 是 前人工作的总结和提炼,通常,被人们广泛流传的设计模式都是针对 某一特定问题 的成熟的解决方案 使用 设计模式 是为了可重用代码.让代码更容易被他 ...
- jquery----jquery中的属性的利用
1.javascript addClass 利用document.getElementById("XX")找到document对象.然后再通过addClass("xxx& ...
- 使用7zip批量压缩文件夹到不同压缩包
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\" ...
- 论文阅读笔记六:FCN:Fully Convolutional Networks for Semantic Segmentation(CVPR2015)
今天来看一看一个比较经典的语义分割网络,那就是FCN,全称如题,原英文论文网址:https://people.eecs.berkeley.edu/~jonlong/long_shelhamer_fcn ...
- 微信支付JSAPI掉不起来支付按钮是什么原因?(原创)
两种可能: 1.支付页面的js参数有问题 2.微信支付的配置有问题,大概率在微信支付授权目录是否有填写正确
- 来一个使用sysbench测试cpu性能的简单脚本
#!/bin/bash for ((i=1; i<16; i++)); do sysbench cpu run --cpu-max-prime=10000 --threads=4 --time= ...
- curl请求https请求
function curl_https($url,$data){ $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_se ...
- es6 箭头函数【箭头表达式】
箭头函数,通过 => 语法实现的函数简写形式,C#/JAVA8/CoffeeScript 中都有类似语法.与函数不同,箭头函数与其执行下文环境共享同一个 this.如果一个箭头函数出现在一个函数 ...