题解 | 小红书推荐系统

小红书推荐系统

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

import java.util.*;

class KeyWord {
    String word;
    int count;

    public KeyWord(String word, int count) {
        this.word = word;
        this.count = count;
    }
}

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();

        Map<String, Integer> map = new HashMap<>();
        String[] words = s.split(" ");

        for (String word : words) {
            map.put(word, map.getOrDefault(word, 0) + 1);
        }

        List<KeyWord> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() >= 3) {
                list.add(new KeyWord(entry.getKey(), entry.getValue()));
            }
        }

        list.sort((o1, o2) -> {
            if (o1.count != o2.count) return o2.count - o1.count;
            else return o1.word.compareTo(o2.word);
        });

        // Collections.sort(list, (o1, o2) -> {
        //     if (o1.count != o2.count) return o2.count - o1.count;
        //     else return o1.word.compareTo(o2.word);
        // });

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).word);
        }
    }
}

全部评论

相关推荐

rbjjj:太杂了吧,同学,项目似乎都没深度,都是api调度耶,分层架构思想没有体现出来了,前端没有前端优化前端工程化体现,后端微服务以及分层架构没体现以及数据安全也没体现,核心再改改,注重于计算机网络,工程化,底层原理吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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