转: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. Flash 0day CVE-2018-4878 漏洞复现

      0x01 前言 Adobe公司在当地时间2018年2月1日发布了一条安全公告: https://helpx.adobe.com/security/products/flash-player/aps ...

  2. Linux分析第六周——进程的描述和进程的创建

    Linux分析第六周--进程的描述和进程的创建 李雪琦+原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...

  3. Python相关资料收集

    读写Excel: http://blog.csdn.net/five3/article/details/7034826http://tech.ddvip.com/2012-10/13515777031 ...

  4. [学习笔记]快速幂&&快速乘

    本质:二进制拆分(你说倍增我也没脾气).然后是一个配凑. 合起来就是边二进制拆分,边配凑. 快速乘(其实龟速):把乘数二进制拆分.利用乘法分配率. 用途:防止爆long long 代码: ll qk( ...

  5. mysql三-1:存储引擎

    一 什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型 ...

  6. 如何调整Flash与div的相互位置

    让flash置于DIV层之下的方法,让flash不挡住飘浮层或下拉菜单,让Flash不档住浮动对象或层的关键参数:wmode=opaque. 方法如下: 针对IE 在<object>< ...

  7. python使用snappy压缩

    今天在网上找了很久,终于找到1个snappy压缩命令行,记录下来: 1.wget https://bootstrap.pypa.io/get-pip.py 2.python ./get-pip.py ...

  8. TestNG指南

    转载自:http://blog.csdn.net/bigapplestar/article/details/7300137 今天突然收到通知,统一改用TestNG写测试用例,开始查这方面的资料,学习一 ...

  9. org.hibernate.HibernateException: getFlushMode is not valid without active transaction

    Spring & Hibernate 整合异常记录: org.hibernate.HibernateException: getFlushMode is not valid without a ...

  10. Js冒泡事件详解及阻止

    Js冒泡机制是指如果某元素定义了事件A,如click事件,如果触发了事件之后,没有阻止冒泡事件,那么事件将向父级元素传播,触发父类的click函数. 如下例所示: <html>     & ...