LyWang

firstmissingpositive

Nov 25th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. class Solution:
  2.     """
  3.    @param A: An array of integers
  4.    @return: An integer
  5.    """
  6.  
  7.     def firstMissingPositive(self, A):
  8.         # write your code here
  9.         if len(A) == 0:
  10.             return 1
  11.         A = set(A)
  12.         m = min(A)
  13.         u = max(A)
  14.         if u < 0:
  15.             return 1
  16.         while True:
  17.             if m-1 not in A and m-1 > 0:
  18.                 return m -1
  19.             if m<1:
  20.                 m += 1
  21.             else:
  22.                 if m in A:
  23.                     if m+1 in A:
  24.                         m += 1
  25.                     else:
  26.                         return m+1
  27.                 else:
  28.                     m += 1
Add Comment
Please, Sign In to add comment