zoukankan      html  css  js  c++  java
  • Codility经典算法题之九:MissingInteger

    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;
            }
        }
     
  • 相关阅读:
    js-添加删除记录-修改
    js-添加删除记录-添加
    js-添加删除记录-删除
    dom增删改
    事件的冒泡
    div随鼠标在浏览器的窗口任意移动
    多选框全选练习
    python-day3
    python-day2
    python-day1
  • 原文地址:https://www.cnblogs.com/chenglc/p/9358952.html
Copyright © 2011-2022 走看看