Solution for missing number


  • 0
    H

    Following is my solution for the missing number. But pass only 16 test cases
    Can anyone tell me why ?

    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            int stepValue = 1;
            if (nums[0] != 0)
            {
                return 0;
            }
            int  currentNumber = nums[0];
            
            for (int i = 1 ; i < nums.size(); i++)
            {
                if (currentNumber + stepValue < nums[i])
                    return currentNumber + stepValue;
                else
                    currentNumber = nums[i];
            }
        }
    };

  • 0
    H

    @jianchao.li.fighter: I see that you created 120 test cases. For me it's a lot for this kind of problem. Seems there are points that i dont find out. Can you help me, pls ?


  • 0
    E

    Hi the problem could be the step value. The vector is not sorted, though not stated clear in the question.


  • 0
    H

    Yeah. It does not work when not sorted. Thanks


Log in to reply
 

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