LeetCode

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