小红书后端

牛客网 - 找工作神器|笔试题库|面试经验|实习招聘内推,求职就业一站解决_牛客网 (nowcoder.com)

小红书推荐系统

题目:

思路:

尝试(标题4)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        Map<String,Integer> map = new HashMap<>();
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            String str = in.next();
            if(!map.containsKey(str)){
                map.put(in.next(),1);
            }else{
                int i = map.get(str);
                map.put(str,i+1);
            }
        }
        for(EntrySet entry : map.get()){
            if(map.get(entry)>=)
        }
    }
}
答案
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 读取输入
        String input = scanner.nextLine();
        // 将输入按空格分割
        String[] words = input.split(" ");
        
        // 使用Map来统计每个单词的出现频率
        Map<String, Integer> wordCount = new HashMap<>();
        for (String word : words) {
            wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
        }
        
        // 使用List来存储满足条件的单词
        List<String> keywords = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
            if (entry.getValue() >= 3) {
                keywords.add(entry.getKey());
            }
        }
        
        // 自定义比较器,首先按频次降序排序,其次按字典序升序排序
        keywords.sort((a, b) -> {
            int freqA = wordCount.get(a);
            int freqB = wordCount.get(b);
            if (freqA != freqB) {
                return freqB - freqA;
            } else {
                return a.compareTo(b);
            }
        });
        
        // 输出结果
        for (String keyword : keywords) {
            System.out.println(keyword);
        }
        
        scanner.close();
    }
}
小结

相关推荐

  1. 2024-07-12 21:28:02       17 阅读
  2. 获得笔记详情 API

    2024-07-12 21:28:02       77 阅读
  3. 运营教程

    2024-07-12 21:28:02       28 阅读
  4. Android实习面经

    2024-07-12 21:28:02       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 21:28:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 21:28:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 21:28:02       58 阅读
  4. Python语言-面向对象

    2024-07-12 21:28:02       69 阅读

热门阅读

  1. 聊聊未来的测试工程师是什么样子?

    2024-07-12 21:28:02       21 阅读
  2. 如何理解Node.js?NPM?Yarn?Vue?React?

    2024-07-12 21:28:02       19 阅读
  3. react多级组件间如何传递props

    2024-07-12 21:28:02       20 阅读
  4. self_attention python代码

    2024-07-12 21:28:02       19 阅读
  5. pytorch 指定GPU设备

    2024-07-12 21:28:02       22 阅读
  6. C#-反射

    C#-反射

    2024-07-12 21:28:02      15 阅读
  7. Codeforces Round #956 (Div. 2) and ByteRace 2024 A-C题解

    2024-07-12 21:28:02       24 阅读
  8. 科技与狠活

    2024-07-12 21:28:02       19 阅读