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 ...
随机推荐
- 记录下在阿里云linux上安装与配置Mysql
环境:阿里云ECS服务器,系统为centos7.2 用户:root 参考博客:https://blog.csdn.net/kunzai6/article/details/81938613 师兄的哈哈哈 ...
- Joone
JOONE 一.什么是JOONE? 1.Joone是一个免费的神经网络框架来创建,训练和测试人造神经网络.目标是为最热门的Java技术创造一个强大的环境,为热情和专业的用户.2.Joone由一个中央引 ...
- JSON字符串自动转换
package com.zkn.newlearn.json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObj ...
- Spring 开发常见问题
linux 下http 接收中文参数乱码 解决: 在application.yml配置文件中添加 spring: http: encoding: charset: GB2312
- CAShapLayer的使用1
1.添加橙色圆环 - (CAShapeLayer *)shapeLayer { if (!_shapeLayer) { _shapeLayer = [CAShapeLayer layer]; CGRe ...
- 微信域名检测的C#实现
背景:最近公司的公众号域名被封了,原因是公司网站被黑后上传了一个不符合微信规范的网页.所以...就进入了微信域名解封的流程. 百度微信域名解封发现很多微信域名检测的网站,还有Api:但是本人做微信公 ...
- 3d-tiles、gltf 坐标系
gltf 为 y 轴向上的右手坐标系 3d-tiles 为 z 轴向上的右手坐标系
- 三大家族(offset、scroll、client)
offset.scroll.client三大家族 offset家族 offsetWidth 与 offsetHeight offset 偏移 用于获取元素自身的位置和大小 offsetWidth和of ...
- IP地址字符串与int整数之间的无损转化
今天鹅厂店面,最后问了一个ip地址字符串和整数间无损转化的问题,晚上有时间了手撸了一下代码. public class IPstr { public static void main(String a ...
- pygame学习之打印文本
import pygame from pygame.locals import * white = 255, 255, 255 blue = 0, 0, 200 pygame.init() scree ...