LeetCode - First Missing Positive

First Missing Positive

2014.2.8 23:43

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.

Solution1:

  The first solution I thought of was hashing, using unordered_map. This promises O(n) time, but not constant space.

  Solution is simple and runs in one pass. I guess the code can explain itself.

  Time and space complexities are both O(n).

Accepted code:

 1 // 1AC, unordered_map is a good substitute for map
 2 #include <unordered_map>
 3 using namespace std;
 4 
 5 class Solution {
 6 public:
 7     int firstMissingPositive(int A[], int n) {
 8         if (A == nullptr || n <= 0) {
 9             return 1;
10         }
11         
12         unordered_map<int, int> um;
13         int result;
14         int i;
15         
16         result = 1;
17         for (i = 0; i < n; ++i) {
18             if (A[i] > result) {
19                 um[A[i]] = 1;
20             } else if (A[i] < result) {
21                 continue;
22             } else {
23                 um[result] = 1;
24                 ++result;
25                 while (um.find(result) != um.end()) {
26                     ++result;
27                 }
28             }
29         }
30         
31         um.clear();
32         return result;
33     }
34 };

Solution2:

  The last solution used hashing, thus cannot satisfy the constraint on space usage.

  Note that there're n integers in [1, n], if there exists some A[i] out of this range, one of the positive integers from [1, n] must be missing, which is the answer we're looking for. If no such A[i] exists, (n + 1) will be the first missing positive integer.

  Since we care only about positive integers, we can use "-" sign to mark a position, as a way of hashing. If we mark A[i] as minus, it means the positive number i has already appeared. You don't have to worry about i going out of range, because the size of the hash table is exactly n. The array itself serves as the hash table, while the sign of each element is used for hashing, whereas their absolute values are preserved.

  Time complexity is O(n). Space complexity is O(1).

Accepted code:

 1 // 1CE, 1AC, this solution is very tricky, hope it would inspire you.
 2 class Solution {
 3 public:
 4     int firstMissingPositive(int A[], int n) {
 5         if (A == nullptr || n <= 0) {
 6             return 1;
 7         }
 8         
 9         int i;
10         for (i = 0; i < n; ++i) {
11             if (A[i] <= 0) {
12                 A[i] = n + 1;
13             }
14         }
15         
16         int tmp;
17         for (i = 0; i < n; ++i) {
18             if (myabs(A[i]) <= n) {
19                 // think about why we care about values between [1, n]
20                 tmp = myabs(A[i]);
21                 // the array itself serves as a hash table of size n.
22                 // setting it to minus means occupying this position.
23                 A[tmp - 1] = -myabs(A[tmp - 1]);
24             }
25         }
26         
27         for (i = 0; i < n; ++i) {
28             if (A[i] > 0) {
29                 return (i + 1);
30             }
31         }
32         // this array consists of [1, n].
33         return (n + 1);
34     }
35 private:
36     int myabs(const int x) {
37         return (x >= 0 ? x : -x);
38     }
39 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3541168.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值