MissingInteger
Find the smallest positive integer that does not occur in a given sequence.
Task Score
100%
Correctness
100%
Performance
100%
이것도 날아가서 의도치 않게 재도전 ^^..!
HashMap
은 key값이 같으면 2번 저장해도 1개만 입력되기 때문에 중복처리를 해줄 필요 없이 그냥 쭉쭉 전체를 입력해주면 된다. 그리고 1부터 A배열의 크기(N)까지 돌려주면서 빈 값을 찾는다. 만약 1부터 N까지의 값이 다 차있을 경우 반복문을 빠져나오고 N+1이 반환된다.
x
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
Arrays.sort(A);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < A.length; i++)
map.put(A[i], A[i]);
for (int i = 1; i <= A.length; i++) {
if (map.get(i) == null)
return i;
}
return A.length + 1;
}
}
'공부 > algorithm' 카테고리의 다른 글
[codility] MaxCounters (0) | 2018.05.23 |
---|---|
[codility] PermCheck (0) | 2018.05.23 |
[codility] FrogRiverOne (0) | 2018.05.23 |
[codility] TapeEquilibrium (0) | 2018.05.22 |
[codility] PermMissingElem (2) | 2018.05.22 |
댓글