First Missing Positive leetcode java
题目:
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0]
return 3
,
and [3,4,-1,1]
return 2
.
Your algorithm should run in O(n) time and uses constant space.
题解:
题目给了一个unsorted integer array。当然,可以用先排好序的方法走(但是时间复杂度就不能是O(n))。
所以这里提供一个不先排序的算法。
注意:题目要求是find the first missing positive integer 。
也就是说,即便你给的数组是4 5 6 7,看似都一一排好序,但是返回值一定是1,也就是如果给的数组是4 5 7 8 ,答案不是6,是1。
因此,有了这个性质,我们就能i和A[i]是否相等来做判断了。“实现中还需要注意一个细节,就是如果当前的数字所对应的下标已经是对应数字了,那么我们也需要跳过,因为那个位置的数字已经满足要求了,否则会出现一直来回交换的死循环。”引自 Code Ganker
代码如下:
1 private void swap(int[] A, int i, int j){
2 if(A[i]!=A[j]){
3 A[i]^=A[j];
4 A[j]^=A[i];
5 A[i]^=A[j];
6 }
7 }
8 public int firstMissingPositive(int[] A) {
9 if(A.length==0||A==null)
return 1;
for (int i = 0; i < A.length; i++){
if (i != A[i]){
if (A[i] <= 0 || A[i] > A.length-1 || A[i] == A[A[i]])
continue;
else{
swap(A, i, A[i]);
i--;
}
}
}
int k = 1;
while (k < A.length && A[k] == k)
k++;
if(A[0]==k)
return k+1;
else
return k;
}
一个更易于理解的代码来自于codeganker:
1 public int firstMissingPositive(int[] A) {
2 if(A==null || A.length==0)
3 return 1;
4
5 for(int i=0;i<A.length;i++){
6 if(A[i]<=A.length && A[i]>0 && A[A[i]-1]!=A[i]){
7 int temp = A[A[i]-1];
8 A[A[i]-1] = A[i];
9 A[i] = temp;
i--;
}
}
for(int i=0;i<A.length;i++){
if(A[i]!=i+1)
return i+1;
}
return A.length+1;
}
First Missing Positive leetcode java的更多相关文章
- First Missing Positive -- LeetCode
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Java [Leetcode 41]First Missing Positive
题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [LeetCode]题解(python):041-First Missing Positive
题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- LeetCode OJ 41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
随机推荐
- java 获取当前方法名
String _thisMethodName = new Exception().getStackTrace()[0].getMethodName();// 获得当前的方法名
- JFreeChart 之折线图
JFreeChart 之折线图 一.JFreeChart 简介 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications, applets ...
- bzoj4289 Tax
Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...
- Jmeter关于上传图片接口
最近接到的一个新的项目,老规矩,开发组开发完接口需要进行接口的测试,其他的很简单,根据限制条件逻辑等设计数据,用浏览器或者工具进行验证就OK. 其中有一个接口涉及到图片的上传,以前没有用过,通过查找资 ...
- slf4j 和 log4j合用的(Maven)配置
简述: 添加logger的日志输出,下面是配置信息供备忘 步骤: 1. 在Maven的porn.xml 文件中添加dependency如下 <dependency> <group ...
- FireDAC 下的 Sqlite [1] - 前言
很长时间没静下心来写博客了, 现在回来, 是 Delphi 不断地进步让我感动.振奋. Delphi XE5 并入了 FireDAC, 第一印象非常好, 恐怕 dbExpress 等等都要靠边站了. ...
- oracle直接读写ms sqlserver数据库(二)配置透明网关
环境说明: 数据库版本:11gR2 透明网关版本:11g 操作系统Windows Server2008_64位 ORACLE_HOME目录:D:\app\Administrator\product\1 ...
- 【Go命令教程】13. go tool cgo
cgo 也是一个 Go 语言自带的特殊工具.一般情况下,我们使用命令 go tool cgo 来运行它.这个工具可以使我们创建能够调用 C 语言代码的 Go 语言源码文件.这使得我们可以使用 Go 语 ...
- WinForm基于插件开发实现多项配置存储
一.课程介绍和实例在线演示 明人不说暗话,跟着阿笨一起玩WinForm.本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的一部分,阿笨后续会计划将实际项目中的一些比较实用的关于C ...
- C#编程(四十九)----------队列
队列 1.Queue定义 System.Collections.Queue类表示对象的先进先出集合,存储在Queue(队列)中的对象在一端插入,从另一端移除. 2.优点 (1).能对集合进行顺序处理( ...