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 ...
随机推荐
- Net MVC使用datatables插件
基本用法 1 - 引入js和css <link href="https://cdn.bootcss.com/datatables/1.10.19/css/dataTables.boot ...
- mybatis和hibernate中的懒加载
概念:所谓懒加载就是延时加载,延迟加载.什么时候用懒加载呢,我只能回答要用懒加载的时候就用懒加载.至于为什么要用懒加载呢,就是当我们要访问的数据量过大时,明显用缓存不太合适,因为内存容量有限 ,为了减 ...
- Cocos Creator Animation 组件
使用脚本控制动画 Animation 组件 Animation 组件提供了一些常用的动画控制函数,如果只是需要简单的控制动画,可以通过获取节点的 Animation 组件来做一些操作. 播放 var ...
- 微服务框架——SpringCloud(二)
1.Feign声明式服务调用(负载均衡+熔断器) a.概念:Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign整合了Ribbon和Hys ...
- STM8L052低功耗模式
Stm8L系列单片机的低功耗有五种模式: § wait模式 § Lowpower run模式 § Lowpower wait模式 § Active-haltwith full RTC模式 § Halt ...
- <玩转Django2.0>读书笔记:URL规则和视图
1. 带变量的URL #urls.py from django.urls import path from .view import * urlpatterns = [ path('',index_v ...
- APIO2018 被屠记
占坑 day0 10:40才起床 感觉一点也不好 下午去了趟80中拿牌子然而没有到,白浪费我颓废时间. day0.5 早上第一课讲二分凸优化,有点瞌睡 第二课讲匹配相关,感觉这篇文章涵盖了大部分内容 ...
- 我的第一个chrome浏览器扩展 5分钟学习搞定
注意: 文件名必须是 manifest, ,注意扩展名是json, 新建一个文件夹,然后创建一个文本文件,作为这个扩展程序的配置文件,所以文件名是manifest.json, 感谢https://ww ...
- [转]玩转图片Base64编码
转自:[前端攻略]:玩转图片Base64编码 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 ...
- maven build时报错Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
[INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ ...