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.


【题目分析】

给定一个整数数组,找出数组中第一个缺失的正整数。比如[1,2,0]缺少3,[3,4,-1,1]缺少2。


【思路】

1. 复杂度为O(n2)

在数组中分别查找1,2,3,···,nums.length,返回查找到的第一个不在数组中的值。最坏情况下需要nums.length次遍历数组,平均时间复杂度为O(n2)。

2. 复杂度为O(n)

如何更加简单得处理这个问题呢?在使用固定空间的约束下,我们当然是要在原来的数组上做文章。想法如下:

(1)遍历数组,如果当前元素nums[i]等于i+1,则表示第i+1个正数没有缺失,继续向后遍历;

(2)如果当前元素小于等于零或者大于nums.length,则继续向后遍历;

(3)如果当前元素大于零而且小于等于nums.length,则把nums[nums[i]-1]与nums[i]交换。

这样处理的结果就是我们把每一个正数n都放在了n-1的位置上,然后再从头遍历数组,如果某个位置k不满足nums[k] == k+1,则返回缺失的正数k+1,如果遍历到数组末尾,则返回nums.length+1;


【java代码】

 public class Solution {
public int firstMissingPositive(int[] nums) {
if(nums == null || nums.length == 0) return 1;
int i = 0; while(i < nums.length){
if(nums[i] == i+1) i++;
else if(nums[i] > 0 && nums[i] <= nums.length){
int temp = nums[nums[i] - 1];
if(temp != nums[i]){
nums[nums[i] - 1] = nums[i];
nums[i] = temp;
}
else i++;
}
else i++;
} for(i = 0; i < nums.length; i++)
if(nums[i] != i+1) return i+1;
return nums.length+1;
}
}

LeetCode OJ 41. First Missing Positive的更多相关文章

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

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

  2. LeetCode题解41.First Missing Positive

    41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...

  3. 【一天一道LeetCode】#41. First Missing Positive

    一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...

  4. LeetCode OJ:First Missing Positive (第一个丢失的正数)

    在leetCode上做的第一个难度是hard的题,题目如下: Given an unsorted integer array, find the first missing positive inte ...

  5. leetcode problem 41 -- First Missing Positive

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

  6. 【LeetCode】41. First Missing Positive (3 solutions)

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  7. 【leetcode】41. First Missing Positive

    题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...

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

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

  9. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

随机推荐

  1. hadoop bug 笔记

    1.sqoop从mysql导入数据到hdfs的时候,总是在本地运行,而没有运行在集群上 sqoop  配置文件的问题 在 /usr/lib/sqoop/conf 目录下新增文件 sqoop-env.s ...

  2. CoreJavaE10V1P3.4 第3章 Java的基本编程结构-3.4 变量

    1.在Java中,每一个变量都必须有一个类型,在变量声明是,类型必须在变量名之前.示例如下: double salary; int vacationDays; long earthPopulation ...

  3. ORM了解

    1.hibernate通过读取Hibernate.cfg.xml文件创建SessionFactory,并通过SessionFactory创建Session(开始使用要打开,使用结束要关闭);通过Ses ...

  4. metasploit nessus & db_autopwn

    nessus官网:https://www.tenable.com/products/nessus-vulnerability-scanner 下载地址:https://www.tenable.com/ ...

  5. 【Python】Python中对象管理与垃圾回收中两个很有意思的问题

    再Python中是利用引用计数来实现对象管理和垃圾回收的,即其他对象引用该对象时候,其引用计数加1,反之减1,当引用计数为0时候,被垃圾收集器回收. Python解释器对对象以及计数器的管理分为以下两 ...

  6. 【Python】0/1背包、动态规划

    0/1背包问题:在能承受一定重量的背包中,放入重量不同,价值不同的几件物品,怎样放能让背包中物品的价值最大? 比如,有三件物品重量w,价值v分别是 w=[5,3,2] v=[9,7,8] 包的容量是5 ...

  7. Lua: 给 Redis 用户的入门指导

    转自:http://www.oschina.net/translate/intro-to-lua-for-redis-programmers 可能你已经听说过Redis 中嵌入了脚本语言,但是你还没有 ...

  8. PHP奇怪现象

    <?php $a = 0.29; $b = (int)($a*100); echo $b; //输出28,PHP版本5.6.24 echo 0.1 + 0.2 - 0.3; //输出5.5511 ...

  9. MongoDB本地安装与启用(windows )

    MongoDB的安装与MongoDB服务配置 Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐.Mongo DB很好的实现了 ...

  10. hbase自带mapreduce计数表行数功能

    $HBASE_HOME/bin/hbase org.apache.hadoop.hbase.mapreduce.RowCounter ‘tablename’ mapreduce来计数,很快的!!!