leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0041.cpp (463B)


      1 class Solution {
      2   public:
      3     int firstMissingPositive(vector<int> &nums) const {
      4         const int n = size(nums);
      5         for (int i = 0; i < n; i++)
      6             if (nums[i] <= 0) nums[i] = n + 1;
      7         for (int i = 0; i < n; i++) {
      8             const int num = abs(nums[i]);
      9             if (num <= n) nums[num - 1] = -abs(nums[num - 1]);
     10         }
     11         for (int i = 0; i < n; i++)
     12             if (nums[i] > 0) return i + 1;
     13         return n + 1;
     14     }
     15 };