Task description:
This is a demo task.
Write a function:
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:
Complexity:
Solution:python
def solution(p):
maxv = max(p)
if maxv <= 0 :
return 1
else:
for i in range(1,maxv+1):
if i not in p:
return i
return maxv+1
java
import java.util.Arrays;
class Solution {
//TODO int 放不下-1000000~1000000
public int solution(int[] A) {
Arrays.sort(A);
int maxv = A[A.length - 1];
if (maxv <= 0) {
return 1;
} else {
int i;
for (i = 1; i < (maxv + 1); i++)
if (!check(A, i))
return i;
}
return maxv + 1; } boolean check(int[] A, int b) {
for (int i = 0; i < A.length; i++) {
if (b == A[i])
return true;
}
return false;
}
}