题目

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. 【转】让你彻底搞懂websocket

    一.websocket与http WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系,但HTTP是不支持持久连接的(长连接,循环连接的不算) 首先HTTP有 1 ...

  2. Codeforces 576D Flights for Regular Customers 矩阵快速幂+DP

    题意: 给一个$n$点$m$边的连通图 每个边有一个权值$d$ 当且仅当当前走过的步数$\ge d$时 才可以走这条边 问从节点$1$到节点$n$的最短路 好神的一道题 直接写做法喽 首先我们对边按$ ...

  3. match_parent和fill_parent的区别(转)

    有网友表示对于很多工程中的MATCH_PARENT出现在layout中感到不明白,过去只有FILL_PARENT和WRAP_CONTENT那么match_parent到底是什么类型呢? 其实从Andr ...

  4. [廖雪峰] Git 分支管理(3):分支管理策略

    通常,合并分支时,如果可能,Git 会用 Fast forward 模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制 禁用 Fast forward 模式,Git 就会在 merge 时生 ...

  5. perl数组高级

    1 去除一个数组中的重复元素: 使用grep函数代码片段: 代码: my @array = ( 'a', 'b', 'c', 'a', 'd', 1, 2, 5, 1, 5 ); my %count; ...

  6. WCF中的REST是什么

    基于SOAP消息格式的WCF之所以强大原因之一是因为SOAP消息头的高度扩展性.相应的WS-*协议很多都体现在消息头封装的信息上,包括诸如寻址,需要调用方法名,维护Session的信息等等…… SOA ...

  7. C++ 实践总结

     对于一个应用程序而言,静态链接库可能被载入多次,而动态链接库仅仅会被载入一次. Gameloft面试之错误一 Event: 面试官说例如以下程序是能够链接通过的. class Base { Pu ...

  8. ios7下UISearchBar UITextField 光标不出现的问题

    app支持ios7,在UINavBar 里面加入搜索框,结果光标一直出现不了.在overstackflow网站搜索了一下,竟然有人遇到相同的问题.... 解决办法如下: searchBar.tintC ...

  9. C#编程(二十五)----------接口

    接口 如果一个类派生自一个接口,声明这个类就会实现某些函数.并不是所有的面向对象的语言都支持接口. 例如,有一个接口:IDispoable,包含一个方法Dispose(),该方法又类实现,用于清理代码 ...

  10. java中Double类数字太大时页面正常显示而不要用科学计数法

    /** * 当浮点型数据位数超过10位之后,数据变成科学计数法显示.用此方法可以使其正常显示. * @param value * @return Sting */ public static Stri ...