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;
        }
    }

 

 

相关文章:

  • 2021-10-12
  • 2021-07-25
  • 2022-12-23
  • 2021-10-16
  • 2021-06-20
  • 2021-12-28
  • 2022-01-26
  • 2022-01-31
猜你喜欢
  • 2022-12-23
  • 2021-12-01
  • 2021-06-26
  • 2021-12-18
  • 2021-11-30
  • 2022-12-23
  • 2021-06-18
相关资源
相似解决方案