LeetCode Online Judge 题目C# 练习 - 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.

JAVA 正解如下:

 1 public class Solution {

 2     public int firstMissingPositive(int[] A) {

 3         // Start typing your Java solution below

 4         // DO NOT write main() function

 5  

 6         //put i to A[i-1], so the array looks like: 1, 2, 3, ...

 7  

 8         for (int i = 0; i < A.length; i++) {

 9             while (A[i] != i+1) {

10                 if (A[i] <= 0 || A[i] > A.length || A[i] == A[A[i] - 1]) {

11                     break;

12                 }

13  

14                 int tmp = A[A[i]-1];

15                 A[A[i]-1] = A[i];

16                 A[i] = tmp;

17             }

18         }

19  

20         for (int i = 0; i < A.length; i++) {

21             if (A[i] != i+1) {

22                 return i+1;

23             }

24         }

25         return A.length+1;

26          

27     }

28 }

按照数组里的值换位,然后再撸一次,撸到A[i] != i + 1; 返回此i +1,如果不存在,那第一个Missing 正整数就是数组长度的后面一个数了。

对不起了,下面的做法是错误示范,不能算是constant space!!

 1 public static int FirstMissingPositive(int[] A)

 2         {

 3             BitArray ba = new BitArray(int.MaxValue);

 4             int max = int.MinValue;

 5             foreach (int num in A)

 6             {

 7                 if(num > max) //set the maximum number in A[]

 8                     max = num;

 9                 if(num > 0) //ignore the negative number

10                     ba[num] = true;

11             }

12 

13             //if no positive number in A[], return 1

14             if(max <= 0)

15                 return 1;

16 

17             //retrieve the first missing positive

18             for (int i = 1; i < max; i++)

19             {

20                 if (!ba[i])

21                     return i;

22             }

23 

24             return max + 1;

25         }

代码分析:

  建立一个bit array,迭代input数组,碰到positive的就映射到bit array,同时维持一个max来存放数组中最大的数。

  如果max <= 0, 返回1。

  迭代bit array,找出最小的positive number。 如果没有,返回max后一个数 max + 1。

 1             int firstMissingPositive(int A[], int n) {

 2                 // Start typing your C/C++ solution below

 3                 // DO NOT write int main() function

 4         

 5                 // Find the max value

 6                 int max = 0;

 7                 for (int i = 0; i < n; ++i)

 8                     max = (A[i] >= 0 && A[i] > max) ? A[i] : max;

 9         

10                 if (max <= 0)

11                     return 1;

12         

13                 // Allocate memory of size max bits

14                 unsigned int size = max / 8 + (max % 8 > 0);

15                 // Initialize the memory

16                 // all reset to 0 but the first one

17                 char arr[size];

18                 arr[0] = 1;

19                 for (int i = 1; i < size; ++i)

20                     arr[i] = 0;

21         

22                 // If the given array containing the value,

23                 // set the x-th value to 1;

24                 for (int i = 0; i < n; ++i)

25                 {

26                     if (A[i] <= 0)

27                         continue;

28             

29                     unsigned int index = A[i] / 8;

30                     char mask = A[i] % 8;

31             

32                     arr[index] = arr[index] | (1 << mask);

33                 }

34         

35                 // Check the first missing positive

36                 for (int i = 0; i < size; ++i)

37                     for (int mask = 0; mask < 8; ++mask)

38                         if (((arr[i] >> mask) & 1) == 0)

39                             return (i * 8 + mask);

40         

41                 return max + 1;

42             }

贴一个C++的code,毕竟C++没有内建的bit array。

你可能感兴趣的:(LeetCode)