题目:

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.(Hard)

分析:

如果题目要求是O(n^2)时间复杂度,O(1)空间复杂度,或O(n)时间复杂度和O(n)空间复杂度,问题都是好解的。

前者从1开始,对每个数遍历一遍数组即可,发现没有的返回;

后者开辟一个数组flag,flag[i] == 0 或 1表示是否出现过,遍历一遍把flag填充上,再遍历一遍找到第一个没有出现过的数即可。

题目要求O(n)时间复杂度和O(1)空间复杂度,则要考虑在nums数组本身上面做文章。

考虑通过交换使得nums[i]存 i + 1,这样从头遍历找到第一个不满足的位置即可。

交换方案就是用nums[i]与 nums[nums[i] - 1]交换,直到nums[i] == i + 1或 i 超过数组范围(0...size())。

注意:加nums[i] != nums[nums[i] - 1]判断语句(见代码第6行),否则可能出现死循环。

时间复杂度,因为每次交换都会有一个元素到达正确位置,所以复杂度O(n)

代码:

 class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int i = ;
while (i < nums.size()) {
if (nums[i] > && nums[i] <= nums.size() && nums[i] != i + && nums[i] != nums[nums[i] - ]) { //nums[i] != nums[nums[i] - 1] 否则类似[1,1]
swap(nums[i], nums[nums[i] - ])
}
else {
i++;
}
}
for (int j = ; j < nums.size(); ++j) {
if (nums[j] != j + ) {
return j + ;
}
}
return nums.size() + ;
}
};
 

LeetCode41 First Missing Positive的更多相关文章

  1. [Swift]LeetCode41. 缺失的第一个正数 | First Missing Positive

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

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

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

  3. Leetcode First Missing Positive

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

  4. 【leetcode】First Missing Positive

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

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

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

  6. LeetCode - 41. First Missing Positive

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

  7. LeetCode题解-----First Missing Positive

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

  8. Java for LeetCode 041 First Missing Positive

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

  9. [LeetCode]题解(python):041-First Missing Positive

    题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...

随机推荐

  1. Fedora 14 x64 试用手记

    欢迎大家给我投票: http://2010blog.51cto.com/350944 刊登在: http://os.51cto.com/art/201011/235506.htm FC14桌面使用体验 ...

  2. delphi 2010 导出sql server 数据到DBF乱码问题

    近日,由于业务需要导出sql server 数据到DBF文件,要查询多表记录,并适当处理后生成导出DBF文件,系统使用delphi2010平台开发. 首先按要求在VFP里创建DBF表,字段数有240个 ...

  3. 转】MyEclipse10安装Log4E插件

    原博文出自于:http://www.cnblogs.com/xdp-gacl/p/4231812.html 感谢! 一. Log4E插件下载 下载地址:http://log4e.jayefem.de/ ...

  4. 各种less开发工具

    less是前端开发CSS的神器,但如何让less代码语法高亮,智能提示,快速编译及格式化,这不是一般的IDE的less插件能做到.下面是我搜刮到的一些工具 Codekit - incident57又一 ...

  5. AJAX小练习,防止以后忘记

    <div id="content"> <input id="btnShow" type="button" value=&q ...

  6. 关于select @@IDENTITY的初识

    这句话主要是得到唯一的主键,然后应用于下面的SQL语句 例如代码 StringBuilder strSql=new StringBuilder(); strSql.Append("inser ...

  7. HDU 2256 Problem of Precision (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256 最重要的是构建递推式,下面的图是盗来的.貌似这种叫共轭数. #include <iostr ...

  8. python sleep

    Python Sleep休眠函数 Python 编程中使用 time 模块可以让程序休眠,具体方法是time.sleep(秒数),其中"秒数"以秒为单位,可以是小数,0.1秒则代表 ...

  9. C:预编译指令

    预编译 关于编译 参考 关于宏定义 参考 预编译又称为预处理,是做些代码文本的替换工作处理#开头的指令,比如拷贝#include包含的文件代码,#define宏定义的替换,条件编译等,就是为编译做的预 ...

  10. DevExpress控件使用经验总结

    转自:http://www.cnblogs.com/wuhuacong/archive/2011/08/31/2161002.html