메모장 입니다2
codility] MissingInteger 본문
1. 설명
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.
Assume that:
- 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].
2.소스
// you can also use imports, for example: // import java.util.*; 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) { // write your code in Java SE 8 Arrays.sort(A); if(A[A.length-1]<0) return 1; for(int i=0, bef = 0; i<A.length; i++) { if(A[i]>0) { if(A[i] > bef+1) return bef+1; else bef=A[i]; } } return A[A.length-1]+1; } }
https://codility.com/demo/results/trainingMX67CT-MZ2/
'Study > Programming' 카테고리의 다른 글
codility] MinAvgTwoSlice (0) | 2017.11.07 |
---|---|
codility] CountDiv (0) | 2017.11.07 |
codility] PermMissingElem (0) | 2017.11.01 |
codility] TapeEquilibrium (0) | 2017.11.01 |
C++] vector - 1) 생성, 추가, 참조 (0) | 2017.11.01 |