转:http://blog.csdn.net/nanjunxiao/article/details/12973173

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.

看了半天才意识到这道题目的意思是找到第一个没有出现的正整数。

而且,这道题描述的不太对,应该是给定N个数字的一个数组,N个数字应该是连续的,不连续的数字用一些0或负数代替了,找出第一个不连续的数字。

思路:

虽然不能再另外开辟非常数级的额外空间,但是可以在输入数组上就地进行swap操作。

思路:交换数组元素,使得数组中第i位存放数值(i+1)。最后遍历数组,寻找第一个不符合此要求的元素,返回其下标。整个过程需要遍历两次数组,复杂度为O(n)。

下图以题目中给出的第二个例子为例,讲解操作过程。

class Solution {
public:
void swap(int& a,int& b)
{
int temp=a;
a=b;
b=temp;
}
int firstMissingPositive(vector<int>& nums)
{
int numsSize=nums.size();
if(numsSize<1)
return 1;
int pos=0;
while(pos<numsSize)
{
if(nums[pos]>0&&nums[pos]!=pos+1&&nums[pos]-1<numsSize&&nums[pos]!=nums[nums[pos]-1])
swap(nums[pos],nums[nums[pos]-1]);
else
pos++;
}
for(int i=0;i<numsSize;i++)
{
if(nums[i]!=i+1)
return i+1;
}
return numsSize+1; }
};

  

First Missing Positive——数学类的更多相关文章

  1. [LeetCode] First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  2. Leetcode First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  3. 【leetcode】First Missing Positive

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  4. 【leetcode】First Missing Positive(hard) ☆

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  5. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  6. LeetCode题解-----First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  7. Java for LeetCode 041 First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...

  8. [LeetCode]题解(python):041-First Missing Positive

    题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...

  9. leetcode 41 First Missing Positive ---java

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

随机推荐

  1. java规范(三)-----判空----方法内的为空判断

    一般我们判空或者有判断条件时 都是使用 if(条件成立){ 执行代码 } 这样的逻辑. 但是如果对象的字段很深层次时或者条件很多时就容易形成很多个{}的情况,这样就容易分不出哪个花括号属于哪部分.如下 ...

  2. 数字表格(product)

    Portal -->broken qwq Description ​  求\(\prod\limits_{i=1}^n\prod\limits_{j=1}^m f[gcd(i,j)](mod\ ...

  3. 【codechef】Children Trips

    Portal -->CC_Children Trips Solution (英文题解看得真爽qwq不过写的好详细啊ovo) 首先这题有一个很重要的条件就是边权是\(1\)或者\(2\),所以虽然 ...

  4. 实现了一下Berlekamp-Massey

    //from https://www.cnblogs.com/TSHugh/p/9265155.html //在FP中求固定项数数列的线性递推式 //此递推式严格符合数学定义,故可能在末尾出现一些看起 ...

  5. win平台搭建Lnmp环境

    win10上面安装的phpStudy这些天有时候打不开网页 502,请求一直loading,就算是代码问题我也扶不住,安装个Redis,mongodb都超级麻烦,并且好多都模拟Linux做的东西,最终 ...

  6. 【莫队】【P3834】 【模板】可持久化线段树 1(主席树)

    大家好,我是个毒瘤,我非常喜欢暴力数据结构,于是我就用莫队+分块过了这个题 Solution 发现这个题静态查询资瓷离线,于是考虑莫队. 在这里简单介绍一下莫队: 将所有询问离线后,对原序列分块.按照 ...

  7. jquery动态添加的元素绑定的事件不生效的问题

    我们可以通过 $(document).on('click', '#xxx', callback) 这种形式解决. 原因,一般情况下,我们是通过 $('#xxx').click(callback) 这种 ...

  8. Linux系统之路——如何在CentOS7.2安装MySQL

    一.Mysql 各个版本区别:1.MySQL Community Server 社区版本,开源免费,但不提供官方技术支持.2.MySQL Enterprise Edition 企业版本,需付费,可以试 ...

  9. 如何修改mac 电脑的hosts 文件

    mac 电脑的hosts 文件对于一般用户而言是只读的,对于系统管理员而言是可以修改的. hosts文件所在的路径是: /etc/hosts 因为需要系统管理员所以命令是: 输入密码,然后就可以修改了 ...

  10. 009.C++ const使用

    1.引例 class complex { public: complex(, ) : re (r), im (i) {} complex& operator += (const complex ...