LeetCode: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

寻找数组中缺失的最小正整数


算法1

首先最容易想到的是:先对数组排序,然后在查找,但是这样不满足线性时间要求。以下代码oj还是能通过的

1
2
3
4
5
6
7
8
9
10
11
12
class  Solution {
public :
     int  firstMissingPositive( int  A[], int  n) {
         sort(A, A+n);
         int  k = 1;
         for ( int  i = 0; i < n; i++)
             if (A[i] < k); //为了处理小于1的数 或者 处理连续出现相同的数
             else  if (A[i] != k) return  k;
             else  k++;
         return  k;
     }
};


算法2

使用哈希表来记录某个数字是否出现过(当然也可以使用bitmap)。这样的话空间复杂度和int的最大值有关

1
2
3
4
5
6
7
8
9
10
class  Solution {
public :
     int  firstMissingPositive( int  A[], int  n) {
         unordered_set< int > uset;
         for ( int  i = 0; i < n; i++)
             if (A[i] > 0)uset.insert(A[i]);
         for ( int  i = 1; ;i++)
          if (uset.count(i) == 0) return  i;
     }
};


算法3

注意到大小为n的数组,缺失的最小正整数一定在范围[1,n+1]内,因此改进一下算法2,可以使用大小为n+1的哈希表。空间复杂度是O(n),不符合题意

1
2
3
4
5
6
7
8
9
10
class  Solution {
public :
     int  firstMissingPositive( int  A[], int  n) {
         vector< int > hashtable(n+2, 0); //hashtable[i] = 1表示数字i出现过
         for ( int  i = 0; i < n; i++)
             if (A[i] > 0 && A[i] <= n+1)hashtable[A[i]] = 1;
         for ( int  i = 1; i <= n+1; i++)
             if (hashtable[i] == 0) return  i;
     }
};


算法4

上述算法3中,我们可以用数组本身来充当哈希表。稍微变通一下,在遍历数组的过程中把数字 i 放在A[i-1]的位置。最后如果A[k] != k+1就说明k+1这个数字没有出现过。由于数组的大小是n,因此如果原始数组中的数字是1,2…n,则最后应该返回n+1。

还需要注意的是if中判断条件:A[i] != A[A[i]-1];即如果某个位置A[i]已经放置了i+1或者数字A[i]即将要放入的位置(A[A[i]-1])原本就是A[i],则跳过。这样可以避免出现死循环(如数组[1,2]和[1,1])                          本文地址

1
2
3
4
5
6
7
8
9
10
11
12
class  Solution {
public :
     int  firstMissingPositive( int  A[], int  n) {
         for ( int  i = 0; i < n; )
             if (A[i] > 0 && A[i] <= n && A[i] != A[A[i]-1])
                 swap(A[i], A[A[i]-1]);
             else  i++;
         for ( int  i = 0; i < n; i++)
             if (A[i] != i+1) return  i+1;
         return  n+1;
     }
};





本文转自tenos博客园博客,原文链接:http://www.cnblogs.com/TenosDoIt/p/3770051.html,如需转载请自行联系原作者

目录
相关文章
|
索引
LeetCode 387. First Unique Character in a String
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
88 0
LeetCode 387. First Unique Character in a String
|
测试技术 API
LeetCode 278. First Bad Version
假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。 你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。
48 0
LeetCode 278. First Bad Version
|
测试技术 API
LeetCode 278. 第一个错误的版本 First Bad Version
LeetCode 278. 第一个错误的版本 First Bad Version
|
数据库
LeetCode(数据库)- First and Last Call On the Same Day
LeetCode(数据库)- First and Last Call On the Same Day
89 0
LeetCode之First Unique Character in a String
LeetCode之First Unique Character in a String
103 0
|
Java 索引 Python
LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String
题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字母。
750 0
|
算法
LeetCode 41 First Missing Positive(丢失的第一个正数)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/52435582 翻译 给定一个未排序的整型数组,找出第一个丢失的正数。
1060 0
|
API 移动开发
LeetCode 278 First Bad Version(第一个坏版本)(二分法)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50611643 翻译 你是一个产品经理,目前正在带领团队去开发一个新产品。
873 0