1. MissingInteger 最小遗失整数 Find the minimal positive integer not occurring in a given sequence.

package com.code;

import java.util.Arrays;

public class Test04_1 {
    public static int solution(int[] A) {
        int size = A.length;
        if(size==1){ // handle one element array
            if(A[0]==1){
                return 2;
            }else{
                return 1;
            }
        }
        Arrays.sort(A); // sort by JDK
        if(A[0]>1){ // handle all elements bigger than 1
            return 1;
        }
        if(A[size-1]<=0){ // handle all elements are negative
            return 1;
        }
        int i=0;
        for(i=0;i<size-1;i++){
            if(A[i]>=0  && (A[i+1]-A[i]>1)){ // handle no consecutive array which start with 1
                    return A[i]+1;
                }
        }
        if(i==size-1){ // handle consecutive array , handle negative elements too.
            return A[size-1]+1>0?A[size-1]+1:1;
        }
        return 1;
    }
    public static void main(String[] args) {
        int [] a = {1,1,2,3,4,5};
        System.out.println(solution(a));
        int [] b = {-4,-2,0,5};
        System.out.println(solution(b));
        int [] c = {2};
        System.out.println(solution(c));
        int [] d = {-2,0,100,102,200};
        System.out.println(solution(d));
        int [] e = {2,4,5,6};
        System.out.println(solution(e));

    }
}
/**
 *
 * 1. MissingInteger
  Find the minimal positive integer not occurring in a given sequence.
 * Write a function:
 *
 * class Solution { public int solution(int[] A); }
 *
 * that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer (greater than 0) that does not occur in A.
 *
 * For example, given:
 *
 * A[0] = 1 A[1] = 3 A[2] = 6 A[3] = 4 A[4] = 1 A[5] = 2 the function should return 5.
 *
 * Assume that:
 *
 * N is an integer within the range [1..100,000]; each element of array A is an integer within the range [?2,147,483,648..2,147,483,647]. Complexity:
 *
 * expected worst-case time complexity is O(N); expected worst-case space complexity is O(N),
 * beyond input storage (not counting the storage required for input arguments). Elements of input arrays can be modified.
 *
 */
时间: 2024-10-06 10:29:40

1. MissingInteger 最小遗失整数 Find the minimal positive integer not occurring in a given sequence.的相关文章

4137:最小新整数

查看 提交 统计 提示 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个十进制正整数n(0 < n < 1000000000),每个数位上数字均不为0.n的位数为m.现在从m位中删除k位(0<k < m),求生成的新整数最小为多少?例如: n = 9128456, k = 2, 则生成的新整数最小为12456 输入 第一行t, 表示有t组数据:接下来t行,每一行表示一组测试数据,每组测试数据包含两个数字n, k. 输出 t行,每行一个数字,表示从n中

3528:最小新整数

描述 给定一个十进制正整数n(0 < n < 1000000000),每个数位上数字均不为0.n的位数为m.现在从m位中删除k位(0<k < m),求生成的新整数最小为多少?例如: n = 9128456, k = 2, 则生成的新整数最小为12456 输入第一行t, 表示有t组数据:接下来t行,每一行表示一组测试数据,每组测试数据包含两个数字n, k.输出t行,每行一个数字,表示从n中删除k位后得到的最小整数.样例输入 2 9128456 2 1444 3 样例输出 12456

4.6算法之贪心-3528:最小新整数

描述 给定一个十进制正整数n(0 < n < 1000000000),每个数位上数字均不为0.n的位数为m.现在从m位中删除k位(0<k < m),求生成的新整数最小为多少?例如: n = 9128456, k = 2, 则生成的新整数最小为12456 输入第一行t, 表示有t组数据:接下来t行,每一行表示一组测试数据,每组测试数据包含两个数字n, k.输出t行,每行一个数字,表示从n中删除k位后得到的最小整数.样例输入 2 9128456 2 1444 3 样例输出 12456

寻找最小的整数

题目:给定一个无序的整数数组,怎么找到第一个大于0,并且不在此数组的整数.比如[1,2,0]返回3,[3,4,-1,1]返回2,[1, 5, 3, 4, 2]返回6,[100, 3, 2, 1, 6,8, 5]返回4.要求使用O(1)空间和O(n)时间 解答:不停的让a[i] =i  归位. #include <stdio.h> void Swap(int &a, int &b) { int c = a; a = b; b = c; } int FindFirstNumberN

贪心 - bailian4137:最小新整数

题目链接 http://bailian.openjudge.cn/practice/4137/ 这个题目原本以为选出前k大的数删除就可以得到正确结果,但是这种策略是错的,举一个反例:52376 2,如果取出7 6,得到523,显然还有更小的数 236. 正确的贪心策略是从左到右每次选取一个比后一位大的数,删除,进行k次,如果所有数字是增序的,就删除最后一个数字. 解题代码 #include <cstdio> #include <cstring> char s[15]; int m;

寻找失踪的整数数组(Find the missing integer)

排列a包含N分子,其元素属于[0,N]之间,且不存在反复的元素.请你找出数组中缺失的元素(由于[0,N]之间有N+1个元素.而数组仅仅能存储N个元素.所以必定缺少一个元素).当中对数组的操作满足下列的条件:不能在常数时间内读取数组中的元素,可是可以读取数组中元素的某一个bit值.可以在常数时间内交换数组的两个元素的位置.请设计一种算法使其可以在线性时间内找出数组中缺失的元素. (N=2^k) An array a[] contains all of the integers from 0 to

hdu 5108(数论-整数分解)

Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1847    Accepted Submission(s): 629 Problem Description Alexandra has a little brother. He is new to programming. One

MissingInteger

Task description Write a function: int solution(int A[], int N); that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer that does not occur in A. For example, given:   A[0] = 1       A[1] = 3       A[2] = 6  

BestCoder#19 HDU5108(质因数分解法)

Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1614    Accepted Submission(s): 193 Problem Description Alexandra has a little brother. He is new to programming. One