题目

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的更多相关文章

  1. First Missing Positive -- LeetCode

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  2. Java for LeetCode 041 First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...

  3. leetcode 41 First Missing Positive ---java

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  4. Java [Leetcode 41]First Missing Positive

    题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...

  5. [LeetCode] First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  6. [LeetCode]题解(python):041-First Missing Positive

    题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...

  7. [Leetcode][Python]41: First Missing Positive

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...

  8. LeetCode OJ 41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  9. [array] leetcode - 41. First Missing Positive - Hard

    leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...

随机推荐

  1. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem F. Turning Grille 暴力

    Problem F. Turning Grille 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c70 ...

  2. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem H. Hometask 水题

    Problem H. Hometask 题目连接: http://codeforces.com/gym/100714 Description Kolya is still trying to pass ...

  3. Springboot 线程池配置

    最近的项目里要手动维护线程池,然后看到一起开发的小伙伴直接用Java了,我坚信Springboot不可能没这功能,于是查了些资料,果然有,这里给一下. 首先我们都知道@Async标签能让方法异步执行, ...

  4. 做了一个可定制的英文记忆字典 - RDict

    RDict_1.0 下载 在我自己试用过程中, 随时发现了不少小问题, 我会随时更新下.

  5. Ubuntu下实现软路由(转)

    参考:http://www.openwrt.pro/post-292.html 个人看法: 1.实现路由在Linux下必须要用到iptables进行转发,这才是路由核心. 2.我觉得对于Linux来说 ...

  6. dell T420热插拔安装过程

    http://v.youku.com/v_show/id_XNTUzMjk4NTQw.html

  7. JavaWeb系列之八(Cookie&amp;Session)

     1.jsp的入门     jsp就是一个servlet,终于会被编译成servlet,jsp:java server pages,java服务器端页面,包括html+java+jsp的指令    ...

  8. self.xxx = nil 可以等效于[_xxx release] _xxx= nil 么

    如果属性是copy.retain的话是等价的.如下: - (void)setXXX:(NSString*)axx { if (_xxx != axx) { [_xxx release]; _xxx = ...

  9. arcengine Annotation研究的一些学习资料(转)FeatureWeight

    转自chanyinhelv原文Annotation研究的一些学习资料 下面是我最近对Annotation研究的一些学习资料,收集于此,供大家学习之用. 一.Annotation要素类介绍 在GeoDa ...

  10. 【MySQL】EXPLAIN命令详解

    具体参考: https://www.cnblogs.com/gomysql/p/3720123.html 原文如下: 在工作中,我们用于捕捉性能问题最常用的就是打开慢查询,定位执行效率差的SQL,那么 ...