classSolution {public:intfirstMissingPositive(vector<int>& nums) {if(nums.empty()) {return1; } // Cyclic sortint i =0, n =nums.size();while(i < n) {int j =nums[i] -1;if(j >=0and j < n andnums[i] !=nums[j]) {swap(nums[i],nums[j]); }else{++i; } } // Find the first idx not matching with nums[idx]for(int i =0; i < n; ++i) {if(nums[i] != i +1) {return i +1; } }return n +1; }};
classSolution {public:intfirstMissingPositive(vector<int>& nums) {if(nums.empty()) {return1; } // Cyclic sortint i =0, n =nums.size();while(i < n) {if(nums[i] == INT_MIN) {++i;continue; }int j =nums[i] -1;if(j >=0and j < n andnums[i] !=nums[j]) {swap(nums[i],nums[j]); }else{++i; } } // Find the first idx not matching with nums[idx]for(int i =0; i < n; ++i) {if(nums[i] != i +1) {return i +1; } }return n +1; }};