A little bit slow but very simple (7 lines)


  • 0
    S

    runtime is 4ms and beats 5.78% of cpp submissions

    class Solution {
    public:
    	int firstMissingPositive(vector<int>& nums) {
    		for (auto i = nums.begin(); i != nums.end(); ++i){
    			while (*i>0 && *i <= nums.size() && *i<(i - nums.begin() + 1) && *i != *(nums.begin() + *i - 1))
    				iter_swap(i, nums.begin() + *i - 1);
    		}
    		int k = 0;
    		for (; k < nums.size() && nums[k] == k + 1; ++k);
    		return k + 1;
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.