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.

解题思路一:

刚看到题目的时候感觉无从下手,后来仔细理解题意,需要找到first missing positive integer,这个数肯定是1---nums.length+1的一个值,因此我们可以开一个布尔数组,存储i是否存在,JAVA实现如下:

public int firstMissingPositive(int[] nums) {
boolean[] num=new boolean[nums.length];
for (int i = 0; i < nums.length; i++)
if (nums[i] > 0&&nums[i] <= nums.length)
num[nums[i]-1] = true;
for (int i = 0; i < num.length; i++)
if (!num[i])
return i+1;
return nums.length+1;
}

解题思路二:使用bool数组会有O(N)的空间复杂度,直接采用交换的方法可以避免,JAVA实现如下:

static public int firstMissingPositive(int[] nums)  {
if(nums.length==0)
return 1;
for (int i = 0; i < nums.length; ) {
if (nums[i] >= 0 &&nums[i] < nums.length &&nums[i] != i && nums[i] != nums[nums[i]]){
int temp=nums[i];
nums[i]=nums[nums[i]];
nums[temp]=temp;
}
else i++;
}
for (int i = 1; i < nums.length; ++i)
if (nums[i] != i)
return i;
return nums[0] == nums.length ? nums.length + 1 : nums.length;
}

Java for LeetCode 041 First Missing Positive的更多相关文章

  1. LeetCode 041 First Missing Positive

    题目要求:First Missing Positive Given an unsorted integer array, find the first missing positive integer ...

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

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

  3. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  4. leetcode 41 First Missing Positive ---java

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

  5. Java [Leetcode 41]First Missing Positive

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

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

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  7. 【leetcode】First Missing Positive

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

  8. 【leetcode】First Missing Positive(hard) ☆

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

  9. LeetCode - 41. First Missing Positive

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

随机推荐

  1. 【UVA 1451】Average

    题 题意 求长度为n的01串中1占总长(大于L)的比例最大的一个子串起点和终点. 分析 前缀和s[i]保存前i个数有几个1,[j+1,i] 这段区间1的比例就是(s[i]-s[j])/(i-j),于是 ...

  2. 【转】pageX、clientX、screenX、offsetX、layerX、x

    参考:http://www.cnblogs.com/xesam/archive/2011/12/08/2280509.html chrome: e.pageX——相对整个页面的坐标e.layerX—— ...

  3. 克隆选择算法-python实现

    CSAIndividual.py import numpy as np import ObjFunction class CSAIndividual: ''' individual of clone ...

  4. POJ2288 Islands and Bridges

    Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...

  5. jsp学习(五)

    在进行jsp与jdbc连接时,出现这样一个错误,提示如下: java.net.ConnectException: Connection refused: connect 后来发现是由于mysql数据库 ...

  6. springmvc常用注解标签详解

    1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...

  7. [Angularjs]ng-repeat中使用ng-model遇到的问题

    写在前面 在ng-reapet中如何为ng-model双向绑定呢?在项目中确实遇到这样的问题,绑定了,但是在controller中获取不到它的值,确实挺奇怪的. 系列文章 [Angularjs]ng- ...

  8. hdu 1202 The calculation of GPA

    感觉本题没有什么好解释的,已知公式,直接编程即可. 1.统计所有 科目的学分 得到总学分 2.统计所有 成绩对应的绩点*对应的学分即可(如果成绩==-1直接continue,不进行统计),得到总绩点. ...

  9. pom.xml

    使用intelJ idea 导入maven包管理文件是,使用Import的方式导入,会自动导入pom.xml来导入包. pom.xml会指定父子关系. 例如,总模块的pom.xml中有一下内容: &l ...

  10. 七层负载均衡——HAProxy

    HAProxy入门 HAProxy是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,HAProxy是完全免费的.借助HAProxy可以快速并且可靠的提供基于TCP ...