[Java] 빠진 숫자 찾기 (PermMissingElem)

urzi·2021년 8월 26일
0

코딩테스트

목록 보기
8/20

Type

시간복잡도

내용

주어진 배열에서 N+1의 int array중 하나의 요소가 누락되어 있는데 그걸 찾는다.
배열을 sort해서 순서대로 정렬한 다음에 반복문으로 A[i]와 i+1이 같은지 확인한다.
다르다면 i+1이 정답이다.

import java.util.*;

public int solution(int[] A) {

        int answer = A.length;

        Arrays.sort(A);

        for (int i = 0; i < A.length; i++) {
            if (A[i] != i + 1) {
                return i + 1;
            }
        }

        return answer + 1;
    }
profile
Back-end Developer

0개의 댓글

Powered by GraphCDN, the GraphQL CDN