转: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. hadoop(三)HDFS基础使用

    一.HDFS前言 1. 设计思想          分而治之:将大文件,大批量文件,分布式的存放于大量服务器上.以便于采取分而治之的方式对海量数据进行运算分析     2. 在大数据系统架构中的应用  ...

  2. PID控制算法的C语言实现十一  模糊算法简介

    在PID控制算法的C语言实现九中,文章已经对模糊PID的实质做了一个简要说明.本来打算等到完成毕业设计,工作稳定了再着力完成剩下的部分.鉴于网友的要求和信任,抽出时间来,对模糊PID做一个较为详细的论 ...

  3. Python中scatter()函数--转载

    原博地址:http://blog.csdn.net/anneqiqi/article/details/64125186 最近开始学习Python编程,遇到scatter函数,感觉里面的参数不知道什么意 ...

  4. ctsc2009 移民站选址

    分析:非常非常好的一道题! 首先需要对问题进行转化: 行列无关,对于行单独处理,对于列单独处理 必然存在一个最优方案使得每一个新站与旧站重合. 转化1很显然,对于转化2,是一类非常经典的“中位数问题” ...

  5. windows 安装 apache 服务以及添加 php 解析

    apache 官方并没有提供 windows 的安装包,但是它们官网给出了第三方的链接,我们可以在那些第三方网站上找到适用于 windows 的二进制包. 我们点进去下载一个 64 位的, 下载完之后 ...

  6. 安装vim with python

    http://note.youdao.com/noteshare?id=4eaddfef93696451de7ff890a6af3cc4

  7. LAMP安全加固

    LAMP安全: 1.BIOS:设置BIOS密码,禁用从CD-ROM和软盘引导 2.SSH安全:修改/etc/ssh/sshd_configPermitRootLogin = no //禁止root访问 ...

  8. jQuery中deferred的对象使用

    什么是deferred对象 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组),它们都不是 ...

  9. Fast File System

    不扯淡了,直接来写吧,一天一共要写三篇博客,还有两篇呢. 1. 这篇博客讲什么? Fast File System(FFS)快速文件系统,基本思想已经在在上一篇博客File System Implem ...

  10. 七牛云 PHP SDK服务器鉴权失败!参数解释

    昨天搞了一下午,用7牛官方的SDK demo 1.上传凭证 $policy = array( 'callbackUrl' => 'http://api.example.com/qiniu/upl ...