First Missing Positive

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.

题目意思:

返回第一个缺失的正数。

时间复杂度应该在O(n)且只能使用常数的额外空间。

解题思路:

遍历两遍数组,第一遍的目的是使数组下标为 i 的数值设置为 i+1,如没有 i+1 就是其他值(不在1~n范围的值)。

第二遍遍历的目的是返回第一个不满足下标为 i ,值为 i+1 的地方,return i + 1;

想看图,点我。

代码如下:

 class Solution {
public:
int firstMissingPositive(int A[], int n) {
int i = ;
while(i < n){
if(A[i] != i+ && A[i]> && A[i]<=n && A[A[i]-] != A[i])
swap(A[i],A[A[i]-]);
else
i++;
}
for(i = ; i < n; i++){
if(A[i] != (i+))
return i+;
}
return n+;
}
};

【LeetCode练习题】First Missing Positive的更多相关文章

  1. [array] leetcode - 41. First Missing Positive - Hard

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

  2. 【leetcode】 First Missing Positive

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

  3. leetcode 41 First Missing Positive ---java

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

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

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

  5. 【leetcode】First Missing Positive

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

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

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

  7. LeetCode - 41. First Missing Positive

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

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

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

  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] ...

  10. Java [Leetcode 41]First Missing Positive

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

随机推荐

  1. JavaScript为unicode编码转换为中文

    代码laycode - v1.1 关于这样的数据转换为中文问题,常用的以下方法. 1. eval解析或new Function("'+ str +'")()  str = eval ...

  2. First Missing Positive 解答

    Question Given an unsorted integer array, find the first missing positive integer. For example,Given ...

  3. POJ 3997 Stock Chase

    Stock Chase Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 455   Accepted: 131 Descrip ...

  4. Unity 飞机的子弹轨迹

    最近公司在开发一款儿童打飞机游戏.  策划跟我说能在子弹上加上一些轨迹就好了.  比如 旋转 左右移动呀.然后它就很愉快的跑去截其他游戏的图啦... 我看见图的时候, 解决方案: 1.   使用牛逼的 ...

  5. 跨服务器的sql使用

    由于想从别的服务器上的数据库导入一些数据过来 经网上查阅,得到 select * from openrowset( 'SQLOLEDB', '服务器名字'; '用户名'; '密码',数据库名字.dbo ...

  6. C#操作AD的例子

    一下连接中包含了使用c#对AD操作的各种列子 http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-D ...

  7. What is SignalR and Why Should I Use It?

    原文地址: What is SignalR and why should I use it? When I first heard about SignalR, I was not sure what ...

  8. javascript 高级程序设计学习笔记(面向对象的程序设计) 2

    在调用构造函数时会为实例添加一个指向最初原型的指针,我们可以随时为原型添加属性和方法,并且能在实例中体现出来,但如果是重新了原型对象,那就会切断构造函数与最初原型的联系. function Dog ( ...

  9. MySQL基础之 path环境变量的作用 (科普)

    在谈mysql配置环境变量之前,先谈一下windows环境变量的作用,有时候在windows cmd窗口运行命令时,经常会出现“XXX不是内部或外部命令的提示” 原因是系统没有找到执行相应命令的文件( ...

  10. HDU 1071 - The area

    求曲线和直线围成的面积 求表达式,求积分 #include <iostream> using namespace std; ],y[]; int t; double k,m;//fx1: ...