题解 | 小红背单词

小红背单词

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

import java.util.Scanner;
import java.util.*;
import java.lang.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        LinkedHashMap<String,Integer> lhm=new LinkedHashMap<>();//记录每个单词在没背下前背了几次
        HashMap<String,Boolean>hm=new HashMap<>();//记录单词是否被背下
        int n=in.nextInt();
        int num=0;//背单词的数量
        while(n-->0){
            String str=in.next();      
            if(lhm.containsKey(str)&&hm.get(str)==false){//只有当这个单词已经存在且没背记下才需要增加次数
                int count=lhm.get(str);
                lhm.put(str,count+1);
            }
            else if(!lhm.containsKey(str)){//如果没见过这个单词就加到集合里
              lhm.put(str,1);
              hm.put(str,false);
            }
             if(lhm.get(str)>=num+1){//如果这个单词背的次数达到题目要求则被标记背过,背过的单词即使再遇到也不需要进行次数加一
            hm.put(str,true);
                num++;
            }
        }
        System.out.print(num);
        
    }
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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