Inconsistent test results in custom test and submission, any idea?


  • 0
    H

    Here is my code. It failed on the last test. removeElement( [2,1,3,0,1,1,0,3,3,1,3] , 3). But if i ran with custom test, the results were identical. Any idea?

    class Solution {
        public:
            int removeElement(vector<int>& nums, int val) {
                int n = 0;
                for(int i = 0; i + n < nums.size(); i++)
                {
                    while (nums[i+n] == val)
                        n++;
                    nums[i] = nums[i+n];
                }
                
                return nums.size()-n;
            }
        };

  • 0

    You access nums[i+n] without first checking that i+n is a valid index. That could even crash. In custom test, you're just somehow lucky.


  • 0
    H

    Silly mistake. Thanks.


Log in to reply
 

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