반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

코딜리티 10 본문

이직

코딜리티 10

닉의네임 2022. 7. 24. 03:37
반응형
This is a demo task.

Write a function:

class Solution { public int solution(int[] A); }

that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.

For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.

Given A = [1, 2, 3], the function should return 4.

Given A = [−1, −3], the function should return 1.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..100,000];
each element of array A is an integer within the range [−1,000,000..1,000,000].

 

 

public static int solution(int[] A) {
    // write your code in Java SE 8
    Set<Integer> set = new HashSet<>();
    for (int a : A) {
        set.add(a);
    }
    int i = 1;
    while (true) {
        if (!set.contains(i)) {
            return i;
        }
        i++;
    }
}
반응형

'이직' 카테고리의 다른 글

코딜리티 12  (0) 2022.07.24
코딜리티 11  (0) 2022.07.24
코딜리티 9  (0) 2022.07.23
코딜리티 8  (0) 2022.07.23
코딜리티 7  (0) 2022.07.23
Comments