Why runtime error ?


  • 0
    R

    Hello guys,
    This is my code, should be the same as most of the codes I see in the forum, I wonder why I got runtime error for input of {1} on judge, when I run it locally,it works fine for me, thank you.

    class Solution {
    public:
        int firstMissingPositive(vector<int>& nums) {
            int i =0;
            int n = nums.size();
            while(i < n) {
                if(nums[i] <= 0 || nums[i] > n || 
                   nums[i] == nums[nums[i]-1]) 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;
        }
    };
    

Log in to reply
 

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