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,因此那个缺失的整数只可能的范围[1,n+1]

方法一:需要O(n)的空间,设置一个数组vis用来标记该下标对应的数字是否出现过。

int firstMissingPositive(int* nums, int numsSize) {
int vis[10000];
int i; for(i=1;i<=numsSize;i++){
vis[i]=0;
} for(i=0;i<numsSize;i++){
if(nums[i]>0&&nums[i]<=numsSize){
vis[nums[i]]=1;
}
} for(i=1;i<=numsSize;i++){
if(vis[i]==0){
return i;
}
}
return numsSize+1;
}

方法二:将数组中值在1~n的数组元素放到对应下标为该值减1的地方。例如: A[3]=2,则将A[2-1]与A[3]进行交换。最后遍历数组直到元素值不等于下标值加一,则该下标加一  就是第一个缺失的正整数

int firstMissingPositive(int* nums, int numsSize) {
int i,t; for(i=0;i<numsSize;i++){
while(nums[i]!=(i+1)&&nums[i]>0&&nums[i]<=numsSize){
//保证每个元素回到适当的位置
if(nums[nums[i]-1]==nums[i]){
break;
}
t=nums[nums[i]-1];
nums[nums[i]-1]=nums[i];
nums[i]=t;
}
} for(i=0;i<numsSize;i++){
if(nums[i]!=(i+1)){
return i+1;
}
}
return numsSize+1;
}

  

  

LeetCode题解-----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. [array] leetcode - 41. First Missing Positive - Hard

    leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...

  3. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  4. leetcode 41 First Missing Positive ---java

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

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

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  6. 【leetcode】First Missing Positive

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

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

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

  8. LeetCode - 41. First Missing Positive

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

  9. Java for LeetCode 041 First Missing Positive

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

随机推荐

  1. IBatis 配置一对多

    -------说明-------- IBatis 版本2.0 配置一对多 namespace = testDao ------------------ /** *班级的resultMap *Class ...

  2. struts2进阶篇(4)

    一.使用ActionContext访问Servlet API strtus2提供了一个ActionContext类,该类别称为Action上下文或者Action环境,Action可以通过该类来访问最常 ...

  3. [moka同学笔记]yii2 activeForm 表单样式的修改(二)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABAEAAANXCAIAAADLkdErAAAgAElEQVR4nOzdfWwc953nef6zwO5Zg8

  4. The main concepts

    The MVC application model A Play application follows the MVC architectural pattern applied to the we ...

  5. mybatis hellworld

    用maven来进行搭建项目的~~   1. 搭建环境 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

  6. IOS6学习笔记(四)

    1.GCD设置一个timer计时器 - (void)awakeFromNib { __weak id weakSelf = self; double delayInSeconds = 0.25; _t ...

  7. ASP.NET检测到有潜在危险的 Request.Form 值解决方案汇总

    ASP.NET检测到有潜在危险的 Request.Form 值解决方案汇总 当我们在网站中使用CKEditor等富文本编辑器时,大多都会遇到这样的到警告 这是因为ASP.NET默认开启对页面提交内容的 ...

  8. FME中Cass扩展属性转Shp的方法

    问题:真受不了CAD中的注记,只能方便显示,难于数据交互.好在Cass把属性信息基本写在扩展属性中,但显示又成问题了.此事难两全!我们通过查看实体属性,需要把宗地界线的扩展属性提取出来.即组码为-3, ...

  9. 安卓问题集-Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED

    错误出现原因: 1.没有 AndroidManifest.xml file文件(出现几率较小) 2. 是你在外面修改了包名而在 AndroidManifest.xml file.文件中没有同步过去导致 ...

  10. Python基础(5)--字典

    字典由多个键及与其对应的值构成的对组成(把键值对成为项),每个键和它的值之间用冒号(:)隔开,项之间用逗号(,)隔开,而整个字典由一对大括号括起来.空字典由两个大括号组成:{} 本文地址:http:/ ...