41. First Missing Positive

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.

My Thought

题目大意

给定一个数组,找出第一个丢失的正数。

关键在于 O(n)的时间复杂度和常数的空间复杂度

算法

不能用一般的排序做了,可以借鉴 计数排序 的思想。

对于目标数组 \(A\) ,有长度 \(A.length\),如果 \(A\) 是一个完美不丢失的数组,即有:

\[A[\ ]=\{1,2,3,4,5,...,A.length\}
\]

可以看到这个完美数组有一个性质

\[A[i] = i+1
\]

那么对于不完美的数组,我们可以遍历一次,交换元素位置,使该数组的全部元素尽可能在其完美的位置上。这样排完一遍,我们再遍历排序后的数组,如果找到不满足上述性质的位置,就是第一个缺失的正数。

这样交换只用到了一个整数空间,两遍遍历则是2*O(n)

伪代码

PROCEDURE findMissingPositive(int A[])
for i = 0 to A.length-1 do:
if A[i] <= A.length and A[i]>0 and
A[A[i]-1]!=A[i]
change(A[i],A[A[i]-1])
for i=0 to A.length-1 do:
if A[i]!= i+1
return i+1
return A.length+1

Code(C++ 3ms)

class Solution {
public:
int temp;
int firstMissingPositive(vector<int>& nums) {
if(nums.size()==0)
return 1;
for(int i=0;i<nums.size();++i){
if(nums[i]<=nums.size()&&nums[i]>0&&nums[nums[i]-1]!=nums[i]){
temp = nums[nums[i]-1];
nums[nums[i]-1]=nums[i];
nums[i] = temp;
i--;
}
}
for(int i=0;i<nums.size();++i){
if(nums[i]!=i+1)
return i+1;
}
return nums.size()+1;
}
};

LeetCode题解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

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

  3. leetcode problem 41 -- First Missing Positive

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

  4. LeetCode OJ 41. First Missing Positive

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

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

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

  6. [LeetCode 题解]: First Missing Positive

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

  7. leetCode题解之First Missing Positive

    1.问题描述 2.题解思路 本题的思路是对于数组中每个正的元素,应该将其放到数组中对应的位置,比如元素1 ,应该放在数组的第一个位置.以此类推,最后检查数组中元素值和下标不匹配的情况. 3.代码 in ...

  8. 【leetcode】41. First Missing Positive

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

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

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

随机推荐

  1. 云计算三种服务模式——IaaS、PaaS和SaaS

    云计算的服务模式仍在不断进化,但业界普遍接受将云计算按照服务的提供方式划分为三个大类:SaaS(Software as a Service–软件即服务) PaaS(Platform as a Serv ...

  2. 出错:Failed to convert property value of type 'org.apache.ibatis.session.defaults.DefaultSqlSessionFactory' to required type 'java.lang.String' for property 'sqlSessionFactoryBeanName';

    出错的详细信息: 3 ERROR [http-nio-80-exec-3] org.springframework.web.servlet.DispatcherServlet - Context in ...

  3. iOS开发之获取设备类型

    1.简单判断是否是iPad方法 /** 判断是不是iPad*/ + (BOOL)isiPadDevice { return UIUserInterfaceIdiomPad == [UIDevice c ...

  4. 获取Shell脚本当前的目录

    https://qiushao.net/article/1489983836453?p=1&m=0 SCRIPT_DIR=$(cd $(dirname ${BASH_SOURCE[0]}); ...

  5. Entitas Learning Document

    Entitas Learning Document You can find Entitas project in there Entitas for Unity Github There are a ...

  6. python 的os.getenv("PATH")和os.environ.get("PATH")的区别

    os.environ(x [,x]) raises an exception if the environmental variable does not exist. os.getenv(x) do ...

  7. MYSQL根据节点向上和向下查询所有节点

    WITH cte AS ( SELECT * ,UnitID AS level FROM UnitTable WHERE UnitID=2 UNION ALL SELECT g.*,level+1 F ...

  8. TMS320DM642学习----第六篇(CCS中.dat文件类型详解)

    1.如下为.dat文件中文件头的基本格式: MagicNumber Format StartingAddress PageNum Length [NewFormat] 下面是分别的解释: MagicN ...

  9. unity打包exe中的资源管理

    给美术和产品用unity做一些exe工具,会频频遇到导入导出资源的情况. 首先所有的文件应该放在StreamingAssets文件夹下, 如果需要动态替换贴图,这样美术只要替换default.png就 ...

  10. Navicat Premium 12.1.11.0安装与激活

    本文介绍Navicat Premium 12.1.11.0的安装.激活与基本使用. 博主所提供的激活文件理论支持Navicat Premium 12.0.x系列和Navicat Premium 12. ...