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. 264 Ugly Number II 丑数 II

    编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...

  2. Js上传图片并生成缩略图

    Js上传图片并显示缩略图的流程为 Js选择文件->Jquery上传图片->服务器接收图片流->存储图片->返回结果到Js端->显示缩略图 本文上传图片所用的Js库是aja ...

  3. DIV自动居中

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 328.io流(字符串-练习-复制文本文件一)

    public static void main(String[] args) { // TODO Auto-generated method stub FileReader fr = null; Fi ...

  5. 【sqli-labs】 less64 GET -Challenge -Blind -130 queries allowed -Variation3 (GET型 挑战 盲注 只允许130次查询 变化3)

    双括号整型 http://192.168.136.128/sqli-labs-master/Less-64/?id=1)) or ((1

  6. 对vuex的一点理解

    vuex是vue.js的一个状态管理工具,它适用于解决平行组件之间的数据共享问题.一般情况下,我们更多的是父子组件之间通过props或$emit来实现传值,如何不满足以上情况那只有使用vuex进行解决 ...

  7. cgroup代码浅析(1)

    前置:这里使用的linux版本是4.8,x86体系. cgroup_init_early(); 聊这个函数就需要先了解cgroup. cgroup概念 这个函数就是初始化cgroup所需要的参数的.c ...

  8. Javascript 原型链与constructor

    Javascript中的constructor与prototype 在学习javascript面向对象编程的过程中, constructor和prototype一直让我觉得理解不透,慢慢的学习过程中记 ...

  9. 洛谷——P2236 [HNOI2002]彩票

    P2236 [HNOI2002]彩票 给你$m$个数,从中挑$n$个数,使得这$n$个数的倒数之和恰好等于$\frac{x}{y}$ 常见的剪纸思路: 如果当前的倒数和加上最小可能的倒数和$>$ ...

  10. HDU - 2044 - 一只小蜜蜂...(dp)

    题意: 如题 思路: 仔细观察图 1-4和3-6其实是一样的答案,那么所有的方案都可以相减,意思为全部转化为从1开始 剩下的就是观察规律,仔细观察5号,能到5号蜂房的只有3和4,3和4到5号蜂房只有一 ...