zoukankan      html  css  js  c++  java
  • [Leetcode 96] 41 First Missing Positive

    Problem:

    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.

    Analysis:

    At first thought, hash table can be used in this problem. First hash all the numbers in the given array with O(n) time complexity, then search from 1 until the first missing positive number which also takes O(n) time complexity. But the requirement only allows constant space, so this is not good enough. And the way we can solve the problem is to use some in-place method.  The method is as follows: everytime, swap the current element with value a with the A[a-1] place. Thus, A[0] stores 1, A[1] stores 2, etc. After the swap, if the new element in this palce is less than or equal to 0 or happened to be the element should be here, then we move to the next position, else we continue to swap at this position.

    The skip condition is as follows: 1. A[i] <=0; 2. A[i] == i+1; 3. A[i] >= n; 4. A[i] == A[A[i]-1] 

    Code:

    Hashtable Version:

     1 class Solution {
     2 public:
     3     int firstMissingPositive(int A[], int n) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         unordered_set<int> us;
     7         
     8         for (int i=0; i<n; i++) {
     9             us.insert(A[i]);
    10         }
    11 
    12         int ms=1;
    13         while (ms <= n) {
    14             if (us.find(ms) == us.end())
    15                 return ms;
    16             
    17             ms++;
    18         }
    19 
    20         return ms;
    21     }
    22 };
    View Code

    Swap Version:

     1 class Solution {
     2 public:
     3     int firstMissingPositive(int A[], int n) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int tmp;
     7         
     8         for (int i=0; i<n; ) {
     9             if (A[i] > 0 && A[i] != i+1 && A[i] < n) {
    10                 tmp = A[A[i]-1];
    11                 A[A[i]-1] = A[i];
    12                 A[i] = tmp;
    13             }
    14             
    15             if (A[i] <= 0 || A[i] == i+1 || A[i] == A[A[i]-1] || A[i] >=n)
    16                 i++;
    17         }
    18         
    19         int i=0;
    20         while (i<n) {
    21             if (A[i] != i+1)
    22                 return i+1;
    23 
    24             i++;
    25         }
    26                 
    27         return i+1;
    28     }
    29 };
    View Code
  • 相关阅读:
    SVN打tag
    validate命令---rman进行备份和回复的验证
    通达OA 小飞鱼工作流在线培训教程(一)HTML基础介绍
    How to improve Java&#39;s I/O performance( 提升 java i/o 性能)
    mybatis批量插入、批量删除
    Java測试覆盖率工具----Cobertura,EclEmma
    Java Secret: Using an enum to build a State machine(Java秘术:用枚举构建一个状态机)
    灵活数据源的固定行列交叉报表的制作
    HDFS学习笔记(1)初探HDFS
    JPEG压缩图像超分辨率重建算法
  • 原文地址:https://www.cnblogs.com/freeneng/p/3240342.html
Copyright © 2011-2022 走看看