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. 项目中的Git七步精髓

    项目中Git常用的七步操作: 1.git branch -a  查看所有分支 2.git checkout dev_yxq 如果冲突了,操作回退上一个版本, git status git checko ...

  2. git私有仓库与pycharm联合使用

    文章目录 1 创建git私有仓库和pycharm的使用 1.1 克隆私有仓库到本地 1.2 使用pycharm打开 1.3 添加.gitignore文件 1.4 并将其添加到仓库 1.5 提交和推送 ...

  3. 小程序+node+mysql做的小项目

    git源码地址: https://github.com/songkangle/weixin_node 小程序页面 数据库 user表 dream表 node的express框架index.js var ...

  4. JAVA取数两个数组交集,考虑重复和不重复元素

    1.考虑不重复元素,重复元素不添加 import java.awt.List; import java.util.ArrayList; import java.util.TreeSet; public ...

  5. ASP.NET MVC 执行流程介绍

    Routing 组件   Controller   Controller中可用的ActionResult MVC-View(使用的抽象工厂模式的视图引擎) 视图模型

  6. zabbix4.0监控Mysql数据库

    #wget http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.11-1.el7.x86_64.rpm #rpm -ivh ...

  7. Sublime插件:Terminal

    这几天在window环境下用gulp构建前端工程,切来切去浪费了不少时间(右键sublime菜单打开文件所在目录,然后去项目根目录,右键打开cmder).这点webstorm自带的Terminal真的 ...

  8. UOJ#374. 【ZJOI2018】历史 贪心,LCT

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ374.html 题解 想出正解有点小激动. 不过因为傻逼错误调到自闭.不如贺题 首先我们考虑如何 $O(n ...

  9. 指针*p,p,&p等辨别

    #include<iostream> #include<iomanip> #include<cmath> using namespace std; int main ...

  10. 纯 CSS 绘制三角形(各种角度)

     转载:https://www.cnblogs.com/lhb25/p/css-and-css3-triangle.html       Triangle Up #triangle-up { widt ...