当前位置:网站首页>比较两个对象的大小关系原来可以如此花里胡哨
比较两个对象的大小关系原来可以如此花里胡哨
2022-06-26 18:01:00 【一只懐坏旭】
一,先看一下题目
给一非空的单词列表,返回前 k 个出现次数最多的单词。
返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。
看完题目你是不是已经有了想法??????
TopK???
NO!!!
(慢慢品)
二,解题思路
1.借助Map统计各个单词的出现次数
2.把键值对组织到一个ArrayList中
3.按照题目要求进行降序排序(出现次数+字典序)
不多说!!!!下面才是重点
三,上代码(看点在这里)
先把统计结果放入一个ArrayList
public List<String> topKFrequent(String[] words,int k){
//先统计各单词出现次数
Map<String,Integer> map = new HashMap<>();
for (String s : words ) {
int count = map.getOrDefault(s,0);//获取到当前单词出现次数
map.put(s,count+1);//存入map中
}
//把刚才统计到的字符串放到ArrayList中
//KeySet相当于得到了一个Set,Set中存放的就是所有的key
ArrayList<String> arrayList = new ArrayList(map.keySet());
//按照刚才的统计次数,针对ArrayList进行排序接下来就开始表演了
1.使用集合类Collections自带的sort方法对ArrayList进行排序
注意: sort默认按元素自身大小进行升序排序(String的字典序) 此处我们要按照字符串出现次数降序排序,也就需要比较器自定制比较规则
思考:这里我们用Comparable行不行???
答案:不行 原因:因为我们是针对String进行制定比较规则,要比较就要更改String源码,你能改吗?
所以我们得重新写一个比较器,来指定String什么算大,什么算小
static class MyComparator implements Comparator<String>{
private Map<String,Integer> map;
public MyComparator(Map<String, Integer> map) {
this.map = map;
}
@Override
public int compare(String o1, String o2) {
int count1 =map.get(o1);
int count2 = map.get(o2);
if(count1 == count2){
//String自身实现了Comparable,自带字典序的比较功能
//CompareTo就是使用String默认的比较规则
return o1.compareTo(o2);
}
return count2-count1;
}
}
public List<String> topKFrequent(String[] words,int k){
//先统计各单词出现次数
Map<String,Integer> map = new HashMap<>();
for (String s : words ) {
int count = map.getOrDefault(s,0);//获取到当前单词出现次数
map.put(s,count+1);//存入map中
}
//把刚才统计到的字符串放到ArrayList中
//KeySet相当于得到了一个Set,Set中存放的就是所有的key
ArrayList<String> arrayList = new ArrayList(map.keySet());
Collections.sort(arrayList,new MyComparator(map));
return arrayList.subList(0,k);
}2.使用匿名内部类(语法糖)
什么时候用到内部匿名类??
这个类只用一次,用完就丢了
public List<String> topKFrequent(String[] words,int k){
//先统计各单词出现次数
Map<String,Integer> map = new HashMap<>();
for (String s : words ) {
int count = map.getOrDefault(s,0);//获取到当前单词出现次数
map.put(s,count+1);//存入map中
}
//把刚才统计到的字符串放到ArrayList中
//KeySet相当于得到了一个Set,Set中存放的就是所有的key
ArrayList<String> arrayList = new ArrayList(map.keySet());
Collections.sort(arrayList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int count1 = map.get(o1);
int count2 = map.get(o2);
if(count1 == count2){
return o1.compareTo(o2);
}
return count2-count1;
}
});
return arrayList.subList(0,k);
}3.使用lambda表达式(语法糖)
lambda表达式,本质上就是一个匿名方法
public List<String> topKFrequent(String[] words,int k){
//先统计各单词出现次数
Map<String,Integer> map = new HashMap<>();
for (String s : words ) {
int count = map.getOrDefault(s,0);//获取到当前单词出现次数
map.put(s,count+1);//存入map中
}
//把刚才统计到的字符串放到ArrayList中
//KeySet相当于得到了一个Set,Set中存放的就是所有的key
ArrayList<String> arrayList = new ArrayList(map.keySet());
Collections.sort(arrayList, (o1, o2) ->{
int count1 = map.get(o1);
int count2 = map.get(o2);
if(count1 == count2){
return o1.compareTo(o2);
}
return count2-count1;
});
return arrayList.subList(0,k);
}这两颗糖是硬糖,得慢慢品
边栏推荐
- 力扣每日一题-第28天-566.重塑矩阵
- LeetCode——226. 翻转二叉树(BFS)
- 国信证券怎么开户?通过链接办理股票开户安全吗
- 背包问题求方案数
- MySQL index
- [code Capriccio - dynamic planning] t583. Deleting two strings
- QPushButton 样式使用示例(以及按钮setmenu添加下拉菜单的方法)
- Detailed explanation of asymmetric cryptosystem
- Discussion and generation of digital signature and analysis of its advantages
- [QNX] Command
猜你喜欢
随机推荐
Decision tree and random forest
Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
vutils.make_grid()与黑白图像有关的一个小体会
在国金证券开户怎么样?开户安全吗?
Detailed explanation of MySQL mvcc mechanism
transforms.RandomCrop()的输入只能是PIL image 不能是tensor
in和exsits、count(*)查询优化
17.13 补充知识、线程池浅谈、数量谈、总结
How to open a stock account? Is it safe to open an account online now?
MySQL exports all table indexes in the database
vue--vuerouter缓存路由组件
【Unity】在Unity中使用C#执行外部文件,如.exe或者.bat
离婚协议中的几个重点
数据加密标准DES安全性
#26class中get和set设置
Static registration and dynamic registration of JNI
vutils. make_ A little experience of grid () in relation to black and white images
Solve the problem that each letter occupies a space in pycharm
MySQL的MVCC机制详解
丰富专业化产品线, 江铃福特领睿·极境版上市








