카테고리 없음
[Codility] PermMissingElem - Java
호비_hobi
2022. 2. 22. 22:29
1. 정렬한 뒤 앞 뒤 값 비교해서 같지 않으면 i+1 반환
2. 다 돌았는데도 조건 처리 되지 않으면 최종 a.length + 1 반환
* 이렇게도 처리 가능
((1+(a.length+1)) * (a.length + 1)) / 2 - sum(a) = 없는 값
https://app.codility.com/demo/results/training78ZKG6-HRT/
Test results - Codility
An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. Your goal is to find that missing element. Write a function: class Solution { public int solutio
app.codility.com
import java.util.*;
class Solution {
public int solution(int[] A) {
Arrays.sort(A);
for (int i=0; i<A.length; i++) {
if (i+1 != A[i]) {
return i+1;
}
}
return A.length + 1;
}
}
깔끔한 코드 참고