题目

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. JavaScript实现链式调用

    学习Jquery的时候,我们通常会看到链式调用的写法 $(window).addEvent('load', function(){ $('test').show().setStyle('color', ...

  2. DIV+javascript实现首尾相连循环滚动效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 《Go语言实战》Go 类型:基本类型、引用类型、结构类型、自定义类型

    Go 语言是一种静态类型的编程语言,所以在编译器进行编译的时候,就要知道每个值的类型,这样编译器就知道要为这个值分配多少内存,并且知道这段分配的内存表示什么. 提前知道值的类型的好处有很多,比如编译器 ...

  4. 【Unity 3D】碰撞检测

    在unity3d中,能检测碰撞发生的方式有两种, 碰撞器 触发器 概念:     (一)碰撞器是一群组件,它包含了很多种类,比如:Box Collider,Capsule Collider等,这些碰撞 ...

  5. ProFTPd Local pr_ctrls_connect Vulnerability - ftpdctl 漏洞及攻击代码分析

    攻击代码网址:http://www.exploit-db.com/exploits/394/ 1.执行环境: 1.ProFTPD 1.3.0/1.3.0a 2.编译ProFTPD时.--enable- ...

  6. delphi 启动停止windows服务 转

    http://blog.csdn.net/haiou327/article/details/6106233 不用cmd用delphi如何实现启动停止windows服务建议参考一下Delphi的Sckt ...

  7. Delphi XE5 Android 运行黑屏卡死的解决方法

    1. 确保正确安装Android SDK: 开始菜单 > 所有程序 > Embarcadero RAD Studio XE5 > > Android Tools > 打开 ...

  8. Windows Phone本地数据库(SQLCE):1、介绍(翻译)(转)

    一只大菜鸟,最近要学习windows phone数据库相关的知识,找到了一些比较简短的教程进行学习,由于是英文的,顺便给翻译了.本身英语水平就不好,估计文中有不少错误,如果有不幸读到的童鞋请保持对翻译 ...

  9. 【spring cloud】【spring boot】网管服务-->配置文件添加endpoints.enabled = false,SpringBoot应用监控Actuator使用的安全隐患

    转载:https://xz.aliyun.com/t/2233 ==================================================================== ...

  10. String Matching(poj1580)

    /*String Matching Description It's easy to tell if two words are identical - just check the letters. ...