c code(0ms), In place sorting with O(n) time complexity


  • 0
    T
    void swap(int *a, int *b) {
        int tmp = *a;
        *a = *b;
        *b = tmp;
    }
    
    int firstMissingPositive(int* nums, int numsSize) {
        int i;
        while(i<numsSize) {
            if(nums[i]>=1 && nums[i]<=numsSize) {
                if(i+1!=nums[i] && nums[i] != nums[nums[i]-1]) {
                    swap(&nums[i], &nums[nums[i]-1]);
                    continue;
                }
            }
            i++;
        }
        for(i=0; i<numsSize; i++) 
            if(i+1!=nums[i])
                return i+1;
        return numsSize+1;
    }
    

Log in to reply
 

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