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.

这道题求的是在一个乱序数组中缺少的第一个正数,要求时间复杂度o(n)空间复杂度为1。

先上代码

package leetcode;

public class firstMissingPositive {
public int firstMissingPositive(int[] nums) {
int len = nums.length;
int i = 0;
while( i<len){
if( nums[i] == i+1 || nums[i]<0 || nums[i] > len+1)
i++;
else if( nums[i] != nums[nums[i]-1])
swap(nums,i,nums[i]-1);
else
i++;
}
i = 0;
while(i<len && nums[i] == i+1)
i++;
return i+1;
} public void swap(int[] nums, int a,int b){
int num = nums[a];
nums[a] = nums[b];
nums[b] = num;
}
/*
* 1.求第一个缺少的正数,想通思路很简单。
*/
}

这道题虽然是hard,难点就是在于时间和空间复杂度,但是算法并不困难。

就是将每一个在1-len之间的正数放在num[i]上,然后遍历一次,遇到的第一个不一样的数就是结果。

leetcode 41 First Missing Positive ---java的更多相关文章

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

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

  2. LeetCode - 41. First Missing Positive

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

  3. Java [Leetcode 41]First Missing Positive

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

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

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

  5. [leetcode]41. First Missing Positive第一个未出现的正数

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

  6. [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)

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

  7. leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

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

  8. 41. First Missing Positive (JAVA)

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

  9. LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)

    题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description   给出一个未排序的数组,求出第一个丢失的正数. ...

随机推荐

  1. 基于Lumisoft.NET组件的POP3邮件接收和删除操作

    From: http://www.cnblogs.com/wuhuacong/archive/2013/05/06/3063093.html Lumisoft.NET组件是一个非常强大的邮件发送.邮件 ...

  2. 戴文的Linux内核专题:04安全

    转自Linux中国 Linux内核是所有Linux系统的核心.如果有任何恶意代码控制或破害了内核的任何一部分,那么系统会严重受损,文件可能被删除或损坏,私人信息可能被盗等等.很明显,保持内核安全涉及到 ...

  3. [vijos P1512] SuperBrother打鼹鼠

    这周好好码树状数组和线段树!!之前没写过二维树状数组,凭借一维的思路居然写了个比较像模像样的东西出来,原来我没那么脑残.唯一要注意的就是getsum四个矩形加减的边界条件,这里看了别人标程才意识到错误 ...

  4. 对项目的测试--Resharper

    初学 这里做个记录. 1:安装后,Resharper会用他自己的英文智能提示,替换掉 vs2010的智能提示,所以我们要换回到vs2010的智能提示 2:快捷键.是使用vs2010的快捷键还是使用 R ...

  5. SharePoint 2016 的新特性概览(一)(What's New for IT Professionals in SharePoint Server 2016)

    博客地址:http://blog.csdn.net/FoxDave 今天看霖雨大神的转的微软最新的关于SharePoint 2016的Update,正好看到了SP2016新发布的视频,整理一下发出 ...

  6. Be a person

    做人不能太实诚 尤其是干我们这行的 多久时间能做完 你自己心里要有个估算 然后把时间再往后延 别他妈给自己找罪受

  7. osgearth+vs2010安装

    转自:http://www.cnblogs.com/eaglezhao/archive/2011/09/26/2192389.html OSGEARTH + VS2010 安装 *VS 平台不重要,本 ...

  8. oracle触发器如何使用2

    触发器 是特定事件出现的时候,自动执行的代码块.类似于存储过程,但是用户不能直接调用他们.触发器是许多关系数据库系统都提供的一项技术.在ORACLE系统里,触发器类似过程和函数,都有声明,执行和异常处 ...

  9. bind,call,apply区别

     js中bind.call.apply函数的用法 2015-02-27 21:16:39 标签:javascript js bind call apply 原创作品,允许转载,转载时请务必以超链接形式 ...

  10. pl/sql Developer 9注册码

    Product Code:46jw8l8ymfmp2twwbuur8j9gv978m2q2duserial Number:307254password:xs374ca