1144 The Missing Number(JAVA)

1144 The Missing Number(20 分)

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.
Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​5​​). Then N integers are given in the next line, separated by spaces. All the numbers are in the range of int.
Output Specification:

Print in a line the smallest positive integer that is missing from the input list.
Sample Input:

10
5 -25 9 6 1 3 4 2 5 17

Sample Output:

7

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a;
        Boolean[] book = new Boolean[(int) (1e5+10)];
        for (int i = 0; i < 1e5+10; i++) {
            book[i] = false;
        }
        for (int i = 0; i < n; i++) {
            a = sc.nextInt();
            if(a > 0 && a <= 1e5+10) {
                book[a] = true;
            }
        }
        for (int i = 1; i <= n+5; i++) {
            if (book[i] == true) {
                continue;
            }
            else {
                System.out.println(i);
                break;
            }
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_39557517/article/details/81874666