aboutsummaryrefslogtreecommitdiff
path: root/first_missing_pos.cpp
blob: 5955dedb39bf92e9821a2e59151eec19818f04ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "leetcode.h"

class Solution {
public:
  int firstMissingPositive(vector<int> &nums) {
    int n = nums.size();
    for (int i = 0; i < n; i++)
      while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i])
        swap(nums[i], nums[nums[i] - 1]);
    for (int i = 0; i < n; i++)
      if (nums[i] != i + 1)
        return i + 1;
    return n + 1;
  }
};

int main() {}