LeetCode题解41.First Missing Positive
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\) 是一个完美不丢失的数组,即有:
\]
可以看到这个完美数组有一个性质:
\]
那么对于不完美的数组,我们可以遍历一次,交换元素位置,使该数组的全部元素尽可能在其完美的位置上。这样排完一遍,我们再遍历排序后的数组,如果找到不满足上述性质的位置,就是第一个缺失的正数。
这样交换只用到了一个整数空间,两遍遍历则是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的更多相关文章
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- 【一天一道LeetCode】#41. First Missing Positive
一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...
- leetcode problem 41 -- First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode OJ 41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode】41. First Missing Positive (3 solutions)
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- [LeetCode 题解]: First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- leetCode题解之First Missing Positive
1.问题描述 2.题解思路 本题的思路是对于数组中每个正的元素,应该将其放到数组中对应的位置,比如元素1 ,应该放在数组的第一个位置.以此类推,最后检查数组中元素值和下标不匹配的情况. 3.代码 in ...
- 【leetcode】41. First Missing Positive
题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
随机推荐
- C# 高级编程03----细节内容
一.名称空间 1.C#使用Using关键字可以列出所需类的名称控件. 它和C/C++ 中的#include不一样.using语句并没有在这些文件之间建立物理连接 2.使用using给名称空间指定别名 ...
- mysql 函数学习(常用)
1.时间函数(组1) SELECT CURRENT_DATE() AS date, CURRENT_TIME() AS `time`, CURRENT_TIMESTAMP() AS `timestam ...
- canal-随记001-吐血一个下午找bug
前天leader说,阿里的新版本canal支持 canal收集binlog直接发到kafka,你要不研究一下? ok,没问题. 昨天周六,在家搭了套环境.解决centos7安装mysql各种小细节,按 ...
- JsRender实用入门教程
这篇文章主要介绍了JsRender实用入门实例,包含了tag else使用.循环嵌套访问父级数据等知识点,并提供了完整的实例下载,非常具有实用价值,需要的朋友可以参考下 本文是一篇JsRend ...
- WPF自定义轮播控件
闲得蛋疼做了一个WPF制作轮播动画(随机动画),勉强可以看,写个随笔留个脚印. 效果图:
- js属性对象的propertyIsEnumerable方法
定义 每个对象都有一个propertyIsEnumerable()方法.此方法返回一个布尔值,表明指定的属性是否是可枚举. This method can determine whether the ...
- Excel—宏表函数
首先有一个知识点,宏表函数是不能直接在单元格中写公式的,需要先定义一个名称(“公式”选项卡——“定义名称”),然后在“定义名称”中的“定义位置”中写入宏表函数. GET.CELL(调取单元格信息的函数 ...
- Centos发布java的war包后,无法访问发布的工程
分析: 这时候,看你的开启的端口,8080,3306都能访问的话,那么就是你的地址写错了,可是,经测试,c3p0中的web地址是正确的,那么就去看linux中的tomcat的日志文件再tomcat/l ...
- Kali Linux & Metasploit Framework
systemctl enable postgresql.service systemctl start postgresql.service # systemctl status postgresql ...
- Python + Anaconda + vscode环境重装(2019.4.20)
目录 卸载程序 安装Ananconda 检查系统环境变量 更换国内镜像源 设置VS CODE 用户配置及工作环境配置 @(Python + Anaconda + vscode环境重装) 工程目录的使用 ...