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 ...
随机推荐
- SQL 与关系代数
Table of Contents 前言 关系与表 关系代数的基本运算 投影 选择 并运算 集合差运算 笛卡尔积 更名运算 关系代数的附加运算 集合交运算 连接运算 自然连接 内连接 外连接 结语 前 ...
- FlexGrid布局
FlexGrid布局: Grid布局时网格大小是固定的,如果想网格大小不同的界面可以使用FlexGrid布局.FlexGrid是更加灵活的Grid布局.FlexGrid布局类是wx.FlexGridS ...
- 洛谷P2678跳石头(提高)
题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点. 在起点和终点之间,有 N 块岩石( ...
- 选择MariaDB的压缩数据引擎TokuDB
业务运用场景 数据基本不用update, 不频繁的范围查询 数据存储量较大(为以后准备) 选择占用磁盘较小的db 业务对数据库插入操作频繁,为避免影响其它业务,需要将直播业务的DB 独立出来,选择另外 ...
- Hexo博客收录百度和谷歌-基于Next主题
Hexo博客收录百度和谷歌-基于Next主题(应该是比较全面的一篇教程) 我们的博客做出来当然是希望别人来访问,但是Github和Coding都做了防爬虫处理,这样子我们博客可能就无法被搜索引擎收录, ...
- js文字跳动效果
/*! * chaffle v1.0.0 * * Licensed under MIT * Copyright 2013-2014 blivesta * http://blivesta.com */ ...
- 关于tap设备
$QEMU_PATH \ -nographic \ -drive file=./rootfs.ext4,format=raw \ -net nic,vlan=0 -net tap,vlan=0,ifn ...
- java值转递?引用传递?
值传递是传递的是原值的副本,引用传递传递的是原值. 在Java中,如果是基本数据类型,传递的是该参数字面量值的拷贝.如果是引用数据类型,传递的是该参数所引用对象在堆中地址的拷贝. swap(int a ...
- 为什么js获取图片高度的值 都为0
尼玛 这个问题困扰我好久~ 看别人取值都是 img.width 我取到的总是0: 终于发现取图片尺寸的时候 图片还没有加载完毕.所以在 <img id ='sImg' class='thumbI ...
- CF992E Nastya and King-Shamans 解题报告
CF992E Nastya and King-Shamans 题意翻译 给定一个序列 \(a_i\),记其前缀和序列为 \(s_i\),有 \(q\) 个询问,每次单点修改,询问是否存在一个 \(i\ ...