题解 | 小红书推荐系统

小红书推荐系统

https://www.nowcoder.com/practice/e5b39c9034a84bf2a5e026b2b9b973d0

Java

哈希+排序

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String,Integer> hash=new HashMap<>();
        while(in.hasNext()){
            String s=in.next();
            hash.put(s,hash.getOrDefault(s,0)+1);
        }
        List<My> list=new ArrayList<>();
        Set<String> key=hash.keySet();
        for(String k:key){
            Integer v=hash.get(k);
            if(v>=3){
                My my=new My(k,v);
                list.add(my);
            }
        }
        Collections.sort(list,(o1,o2)->{
            if(o1.count!=o2.count){
                return o2.count-o1.count;
            }else{
                return o1.s.compareTo(o2.s);
            }
        });
        for(My my:list){
            System.out.println(my.s);
        }
    }
    
}
class My{
    String s;
    Integer count;
    public My(String s,Integer count){
        this.s=s;
        this.count=count;
    }

    }

全部评论

相关推荐

12-05 18:09
已编辑
广东药科大学 后端工程师
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务