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.
*
*/

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

  1. 【OpenJ_Bailian - 4137】最小新整数 (贪心)

    最小新整数 Descriptions: 给定一个十进制正整数n(0 < n < 1000000000),每个数位上数字均不为0.n的位数为m.现在从m位中删除k位(0<k < ...

  2. Java中最小的整数为什么是-2147483648

    Java中最小的整数为什么是-2147483648 假如只有两位来表示数字,第一位是符号位: 00:0 01:1 11:-1,这个是负数,而且是补码,取反为00,加1成为01,就是-1 10:-2,这 ...

  3. NOI4.6 最小新整数——切山游戏

    描述 给定一个十进制正整数n(0 < n < 1000000000),每个数位上数字均不为0.n的位数为m. 现在从m位中删除k位(0<k < m),求生成的新整数最小为多少? ...

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

    排列a包含N分子,其元素属于[0,N]之间,且不存在反复的元素.请你找出数组中缺失的元素(由于[0,N]之间有N+1个元素.而数组仅仅能存储N个元素.所以必定缺少一个元素).当中对数组的操作满足下列的 ...

  5. 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)

    We define the smallest positive real number as the number which is explicitly greater than zero and ...

  6. counting elements--codility

    lesson 4: counting elements 1. FrogRiverOne 2. PermCheck 3. MissingInteger 4. MaxCounters lesson 4: ...

  7. hdu 5108(数论-整数分解)

    Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  8. Alexandra and Prime Numbers(思维)

    Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  9. sdut3140 A*B(math)

    题目:传送门 题目描述 Your task is to find the minimal positive integer number Q so that the product of digits ...

随机推荐

  1. SVN版本库的备份及迁移

    备份某个版本库:打开控制台窗口 1.备份某个版本库: svnadmin dump myrepos > dumpfile //将指定的版本库导出成文件dumpfile eg:svnadmin du ...

  2. [ CodeForces 515 D ] Drazil and Tiles

    \(\\\) \(Description\) 给出一个\(N\times M\) 的网格,一些位置是障碍,其他位置是空地,求是否存在一个用 \(1\times 2\)的骨牌铺满空地的方案,以及方案是否 ...

  3. P1823 音乐会的等待

    题目描述 N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人.队列中任意两个人A和B,如果他们是相邻或他们之间没有人比A或B高,那么他们是可以互相看得见的. ...

  4. D1-mini esp8266的资料备份

    需要更新esp8266库 http://arduino.esp8266.com/stable/package_esp8266com_index.json 下载好即可.

  5. git Eclipse项目不显示当前分支

    问题: 在Eclipse中,导入新的git项目,在项目上不再显示当前所处的分支,也不再显示修改了哪些文件 解决: 右键选中项目  -->  Team  -->  Share Project ...

  6. Raspberry Pi开发之旅-控制蜂鸣器演奏乐曲

    一.无源蜂鸣器和有源蜂鸣器 步进电机以及无源蜂鸣器这些都需要脉冲信号才能够驱动,这次尝试用GPIO的PWM接口驱动无源蜂鸣器弹奏一曲<一闪一闪亮晶晶>. 无源蜂鸣器: 无源内部没有震荡源, ...

  7. The user specified as a definer ('root'@'%') does not exist 解决方法

    mysql> grant all privileges on *.* to root@"%" identified by "."; Query OK, r ...

  8. android studio使用中碰到Failure [INSTALL_FAILED_OLDER_SDK] 问题

    第一次使用Android studio开发.直接新建一个默认项目运行出现:Failure [INSTALL_FAILED_OLDER_SDK] , 网上很多人说修改build.gradle中的mins ...

  9. HDU_1180_诡异的楼梯_BFS

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1180 诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  10. 网络编程基础_3.APC队列

    APC队列 #include <stdio.h> #include <windows.h> // 保存 IO 操作的结果 CHAR Buffer1[] = { }; CHAR ...