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 ...
随机推荐
- maxItemsInObjectGraph解释
maxItemsInObjectGraph:一个整数,指定要序列化或反序列化的最大项数,可以限制对象图中要序列化的项数.默认的就是65535,当客户端与WebService之间传递的是对象要序列化的个 ...
- Python学习日记之中文支持
解决中文输出错误 在开头添加 # -*- coding: utf-8 -*- 即可
- Sublime——基本操作
基本安装 程序下载地址:https://www.sublimetext.com/ package control安装 View -> Show Console打开控制台或者用快捷键ctrl+~打 ...
- python学习笔记(7)——集合(set)
关键词#1.定义:无序不重复元素集, 基本功能包括关系测试和消除重复元素. 2.关键词:类似dict.只有key无value.常用于集合类数学运算. 3.创建 s=set() #入参可以是list.t ...
- git常用命令和github
工作区:就是你的工作目录 暂存区:它像个缓存区域,临时保存你的改动 版本区:就是你的git仓库 HEAD:相当于一个指针,指向你最近一次提交后的结果 git status 查看状态 git add . ...
- ansible 批量推送ssh秘钥
ansible 批量推送ssh秘钥 参考:http://docs.ansible.com/ansible/authorized_key_module.html # vi /etc/ansible/ho ...
- 如何让一个div里面的div垂直居中?
如何让一个div里面的div垂直居中? 如何让上面灰色有文字那个div和背景图标垂直居中,不管屏幕大小有好大,始终在垂直方向上的中间.上面有整个布局和样式表,谢谢高手指点 CSS3时代当然要用CSS3 ...
- Redis系列(九)--几道面试题
这里只是一点面试题,想了解更多,可以查看本人的Redis系列:https://www.cnblogs.com/huigelaile/category/1461895.html 1.Redis和Memc ...
- Python随笔day03
温故知新: 注释: 单行注释 # 多行注释 ‘’’ ‘’’ 或者 “”” “”” 注意:三个单引号或双引号可以用于表示多行字符串. 判断输入的字符串是否是数字 salary = inpu ...
- Multisim破解教程
转载:http://www.121down.com/article/article_52879.html