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开始第一个不出现的正整数。

要求O(n)的时间复杂度和常数空间复杂度。

思路:

1、方法一:

先排序,然后遍历数组,找出第一个不出现的正整数。但时间复杂度为O(nlogn),不符合要求。

实现如下:

#include <iostream>
#include <algorithm> using namespace std; int firstMissPositive(int A[],int n){
sort(A,A+n,less<int>());
int i=;
while(A[i]<=) i++;
int j=;
while(i<n){
if(i<n- && A[i]==A[i+]) i++;
if(A[i]!=j) break;
i++;
j++;
}
return j;
} int main()
{
int A[]={,,,-,-,,};
int n=sizeof(A)/sizeof(A[]);
cout<<firstMissPositive(A,n);
return ;
}

2、方法二:

对于正整数A[i],如果将它放在数组中满足A[i]=i+1的位置,那么如果当某个位置不满足A[i]==i+1时,则i为第一个不出现的正整数。

  • 遍历数组,
    • 当遇到小于n(n为数组大小)的正整数,如果它满足A[i]=i+1,则跳过,i++,如果不满足则将它交换它属于它的位置,即swap(A[i],A[A[i]-1]);
    • 当遇到小于0或者大于n的数,或者需交换的位置已经有了满足条件的值即A[i]==A[A[i]-1](数组中有重复数字的时候会有这种情况),则跳过,i++,因为没有合适的位置可以跟它们交换。
  • 再次遍历数组,如果A[i]!=i+1,则i为第一个不出现的正整数。

代码如下:

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

(LeetCode 41)First Missing Positive的更多相关文章

  1. LeetCode(41)First Missing Positive

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

  2. (Problem 41)Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  3. 乘风破浪:LeetCode真题_041_First Missing Positive

    乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...

  4. 【LeetCode练习题】First Missing Positive

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

  5. [LeetCode 题解]: First Missing Positive

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

  6. LeetCode OJ:First Missing Positive (第一个丢失的正数)

    在leetCode上做的第一个难度是hard的题,题目如下: Given an unsorted integer array, find the first missing positive inte ...

  7. (LeetCode 78)SubSets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  8. (LeetCode 72)Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  9. 《从零开始学Swift》学习笔记(Day 41)——类的继承

    原创文章,欢迎转载.转载请注明:关东升的博客 Swift中的继承只能发生在类上,不能发生在枚举和结构体上.一个类可以继承另一个类的方法.属性.下标等特征,当一个类继承其他类时,继承类叫子类,被继承类叫 ...

随机推荐

  1. POJ1151 Atlantis 水题 计算几何

    http://poj.org/problem?id=1151 想学一下扫描线线段树,结果写了道水题. #include<iostream> #include<cstdio> # ...

  2. 【BZOJ-3123】森林 主席树 + 启发式合并

    3123: [Sdoi2013]森林 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2738  Solved: 806[Submit][Status] ...

  3. [转]Android使用Application总结

        目录(?)[+]   Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里 ...

  4. ZOJ 3622 Magic Number 打表找规律

    A - Magic Number Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Subm ...

  5. Codeforces Round #281 (Div. 2) A. Vasya and Football 暴力水题

    A. Vasya and Football time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. SCP用法

    scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port ...

  7. js的栈与堆

    JavaScript中基本数据类型和引用数据类型的区别  这是我引用别人的  觉得很好   1.基本数据类型和引用数据类型 ECMAScript包括两个不同类型的值:基本数据类型和引用数据类型. 基本 ...

  8. 丢失或损坏NDF文件如何附加数据库

    在论坛看到有人遇到 NDF文件丢失并且没有备份,所以无法成功附加数据库.在网上也看到过很多回答是如果没有NDF就无法附加成功. 其实我自己测试下来即使没有NDF也是可以成功附加的.但是有条件,丢失的N ...

  9. 乘法器的Verilog HDL实现

    原文链接:http://www.cnblogs.com/shengansong/archive/2011/05/23/2054401.html 1. 串行乘法器  两个N位二进制数x.y的乘积用简单的 ...

  10. MiniGUI ial 移植指南

    MiniGUI ial 移植指南 2.1 ial的定义 ial是一个通用的抽象输入接口,可以输入统一的数据的结构,也就是说在MiniGUI的核心代码里输入的数据的格式是固定的,不管输入设备是鼠标 还是 ...