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 ...
随机推荐
- Sql2008事务日志已满处理
处理方式: USE [master] GO ALTER DATABASE gzl SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE gzl SET ...
- CentOS 7下ElasticSearch集群搭建案例
最近在网上看到很多ElasticSearch集群的搭建方法,本人在这人使用Elasticsearch5.0.1版本,介绍如何搭建ElasticSearch集群并安装head插件和其他插件安装方法. 一 ...
- [ Luogu 4917 ] 天守阁的地板
\(\\\) \(Description\) 定义二元函数\(F(x,y)\)表示,用 \(x\times y\) 的矩形不可旋转的铺成一个任意边长的正方形,所需要的最少的矩形个数. 现在\(T\)组 ...
- vscode使用教程(web开发)
1.安装 进入官网下载https://code.visualstudio.com/ 一直下一步就好了,中间可以选择把软件安装在哪个目录. 2.常用插件安装 a. 进入扩展视图界面安装/卸载 a1.快捷 ...
- [Windows Server 2012] SQL Server 备份和还原方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:SQL S ...
- Sturts2几个常用内建拦截器的介绍
Sturts2几个常用内建拦截器的介绍:1)conversation:这是一个处理类型转换错误的拦截器,它负责将类型转换错误从ActionContext中取出,并转换成Action的FieldErro ...
- adb 命令收藏学习地址
adb 命令相关的网页https://www.cnblogs.com/medsonk/p/8334847.htmlhttps://www.cnblogs.com/medsonk/p/6959658.h ...
- JAVA学习笔记16——线程生命周期
当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态,在线程的生命周期中,它要经过新建(New).就绪(Runnable).运行(Running).阻塞(Blocking)和 ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- JavaScript 面向对象的编程(三) 类的继承
定义父类和子类的继承关系 //声明父类 function SuperClass(){ this.superValue = true; } //为父类添加共有方法 SuperClass.prototyp ...