当前位置:网站首页>比较两个对象的大小关系原来可以如此花里胡哨
比较两个对象的大小关系原来可以如此花里胡哨
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);
}这两颗糖是硬糖,得慢慢品
边栏推荐
猜你喜欢

二分查找法-1

Row lock analysis and deadlock

The difference between round and truncate in SQL (round or truncate)

Digital signature standard (DSS)

MySQL exports all table indexes in the database

DoS及攻击方法详解

Case study of row lock and isolation level

vue--vuerouter缓存路由组件

Vscode usage - Remote SSH configuration description
![[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers
随机推荐
无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!
How sparksql returns a specific day of the week by date -dayofweek function
QPushButton 样式使用示例(以及按钮setmenu添加下拉菜单的方法)
背包问题求方案数
wechat_ Solve the problem of page Jump and parameter transfer by navigator in wechat applet
17.13 supplementary knowledge, thread pool discussion, quantity discussion and summary
Using redis for user access data statistics hyperloglog and bitmap advanced data types
pycharm如何修改多行注释快捷键
Plt How to keep show() not closed
RSA加密解密详解
A little experience of next (ITER (dataloader))
Daily record 2
Li Kou daily question - day 28 -566 Reshape matrix
接水面试题
数字签名标准(DSS)
Chen Qiang: Alibaba's 100 billion level large-scale digital business knowledge map helps business growth
JS cast
DoS及攻击方法详解
Binary search-1
Let torch cuda. is_ Experience of available() changing from false to true