题解 | #序列找数#

序列找数

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

import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Set<Integer> set=new HashSet<>();
        int count=0;
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            set.add(in.nextInt());
            count++;
        }
        for(int i=0;i<count;i++){
            if(!set.contains(i)){
            System.out.println(i);
            }
        }
        //System.out.println(count);
    }
}

利用hashset去重放入,遍历0-n,没有的输出

全部评论

相关推荐

03-20 18:39
已编辑
电子科技大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务