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.
*
*/
1. MissingInteger 最小遗失整数 Find the minimal positive integer not occurring in a given sequence.的更多相关文章
- 【OpenJ_Bailian - 4137】最小新整数 (贪心)
最小新整数 Descriptions: 给定一个十进制正整数n(0 < n < 1000000000),每个数位上数字均不为0.n的位数为m.现在从m位中删除k位(0<k < ...
- Java中最小的整数为什么是-2147483648
Java中最小的整数为什么是-2147483648 假如只有两位来表示数字,第一位是符号位: 00:0 01:1 11:-1,这个是负数,而且是补码,取反为00,加1成为01,就是-1 10:-2,这 ...
- NOI4.6 最小新整数——切山游戏
描述 给定一个十进制正整数n(0 < n < 1000000000),每个数位上数字均不为0.n的位数为m. 现在从m位中删除k位(0<k < m),求生成的新整数最小为多少? ...
- 寻找失踪的整数数组(Find the missing integer)
排列a包含N分子,其元素属于[0,N]之间,且不存在反复的元素.请你找出数组中缺失的元素(由于[0,N]之间有N+1个元素.而数组仅仅能存储N个元素.所以必定缺少一个元素).当中对数组的操作满足下列的 ...
- 为什么实数系里不存在最小正数?(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 ...
- counting elements--codility
lesson 4: counting elements 1. FrogRiverOne 2. PermCheck 3. MissingInteger 4. MaxCounters lesson 4: ...
- hdu 5108(数论-整数分解)
Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- Alexandra and Prime Numbers(思维)
Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- sdut3140 A*B(math)
题目:传送门 题目描述 Your task is to find the minimal positive integer number Q so that the product of digits ...
随机推荐
- 1、DOS基本命令
命令dir能给列出当前目录下面的所有文件.程序和子目录.所有目录(Windows 中称为文件夹)的目录名前面都有一个<DIR>标记.文件和程序名前面显示有这些文件和程序的大小. 想说的是, ...
- FCC 基础JavaScript 练习4
1.另一种数据类型是布尔(Boolean).布尔 值要么是true要么是false, 它非常像电路开关, true 是“开”,false是“关”.这两种状态是互斥的 2.伪代码 if(条件为真){ 语 ...
- 诊断Java中的内存泄露
每次我怀疑有内存泄漏时,我都要翻箱倒柜找这些命令.所以,这里总结一下以备后用: 首先,我用下面的命令监视进程: 1 while ( sleep 1 ) ; do ps -p $PID -o %cpu, ...
- (转)版本管理工具介绍——SVN篇(二)
http://blog.csdn.net/yerenyuan_pku/article/details/72620498 上一篇文章我介绍了一下SVN,以及SVN服务器的安装,相信大家都安装了,接下来我 ...
- Windows提高_2.3第三部分:内核区同步
第三部分:内核区同步 等待函数(WaitForObject) 等待函数的形式 单个:WaitForSingleObject 多个:WaitForMultipleObjects 一个可以被等待的对象通常 ...
- 安卓app测试之启动时间和电量监控
一.启动时间监控 通过adb命令获取 adb shell am start -W /<packageName> /<activityName> 案例: adb shell am ...
- RabbitMQ系列(三)--Java API
基于java使用RabbitMQ 框架:SpringBoot1.5.14.RELEASE maven依赖: <dependency> <groupId>com.rabbitmq ...
- 使用SELECT语句检索数据
使用SELECT语句检索数据select指令适用于SQL数据库SELECT 语句用于从数据库中选取数据.(指令不分大小写,选择的值除名字和一些有特殊意义的字符可不分大小写,from结束时一定要加;) ...
- linux mysql设置远程访问
>mysql -u root -p 选择进入mysql数据库use `mysql`; 查看所有存在的账号和地址.SELECT `Host`,`User` FROM `user`; 现在决定让ro ...
- (转)MySQL中的索引详讲
序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...