First Missing Positive -- LeetCode
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.
思路:该题求得是从1开始的正整数,第一个没有在所给数组中出现的正整数。
因为是从1开始的,我们可以将数组中所有的正整数都移动到它的正确位置,当所有数字都移动完毕后,从头检查第一个没有出现在该出现位置上的正整数就是所求的数字。若所有的数都出现了,则所求的数字就是n + 1,所有数字后面的那个数字。
该算法因为所有数字都是移动到自己的正确位置,因此每个数字都只会被移动一次。复杂度因此是O(n)。
所给数组中如果出现了重复的数字也不要紧,因为随着交换,所有正整数都回到了自己的正确位置后,重复数字会被交换到剩余的位置中去。而这些位置并不是他们的正确位置,因此也能指示出他们所处位置的正数缺失了。
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for (int i = ; i < n; i++)
while (nums[i] > && nums[i] <= n && nums[nums[i] - ] != nums[i])
swap(nums[i], nums[nums[i] - ]);
for (int i = ; i < n; i++)
if (nums[i] != i + )
return i + ;
return n + ;
}
};
First Missing Positive -- LeetCode的更多相关文章
- First Missing Positive leetcode java
题目: Given an unsorted integer array, find the first missing positive integer. For example, Given [1, ...
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [LeetCode]题解(python):041-First Missing Positive
题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 【一天一道LeetCode】#41. First Missing Positive
一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...
- 乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...
- LeetCode OJ:First Missing Positive (第一个丢失的正数)
在leetCode上做的第一个难度是hard的题,题目如下: Given an unsorted integer array, find the first missing positive inte ...
随机推荐
- WIN10把照片查看器设为默认看图软件
WIN10默认是PHOTO,没有以前WIN7的照片查看器好用,要改回来的方法如下: 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Photo ...
- Windows7中如何让python2和python3共存并使用pip
1.下载安装python2和python3 分别下载python2.7.exe.python3.6.exe并安装到C盘.E盘(如图) 2.配置环境变量 打开“系统变量”中的path文本框(如图 ...
- ASP.NET Core [4]: Authentication(笔记)
原文连接:http://www.cnblogs.com/RainingNight/p/authentication-in-asp-net-core.html 在现代应用程序中,认证已不再是简单的将用户 ...
- HDU 4031 Attack (线段树)
成功袭击次数=所有袭击次数-成功防守次数 需要一个辅助pre来记录上一次袭击成功什么时候,对于每个查询,从上一次袭击成功开始,每隔t更新一次. 感觉这样做最坏时间复杂度是O(n^2),这里 说是O(q ...
- System.Activator类
public static ObjectHandle CreateInstance(string assemblyName, string typeName); 该类有一个方法. // // 摘要: ...
- 六、vue侦听属性
$watch 实际上无论是 $watch 方法还是 watch 选项,他们的实现都是基于 Watcher 的封装.首先我们来看一下 $watch 方法,它定义在 src/core/instance/s ...
- Vuex, api, SSR, module
vuex https://vuex.vuejs.org/zh/guide/actions.html 单向数据流 单例模式 & 多个组件共享状态 State & 状态注入 Vue.use ...
- 【bzoj5017】[Snoi2017]炸弹 线段树优化建图+Tarjan+拓扑排序
题目描述 在一条直线上有 N 个炸弹,每个炸弹的坐标是 Xi,爆炸半径是 Ri,当一个炸弹爆炸时,如果另一个炸弹所在位置 Xj 满足: Xi−Ri≤Xj≤Xi+Ri,那么,该炸弹也会被引爆. 现在 ...
- 淀粉质模板 Tree
Tree 题目描述 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K 输入输出格式 输入格式: N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 ...
- linux进程——后台运行的方法
linux进程后台运行的几种方法: 我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败. 如何让命 ...