The OJ answer is wrong?

  • -1

    I got this error:

    Submission Result: Wrong Answer

    Input: [1,2]
    Output: [2]
    Expected: [1,2]

    But as the question requests, we are required to return the new length, not the new array, so why the OJ says "expected [1,2]" ?

  • 1

    OJ also check the contents of the new array so in this case your array's length may be 2 but check whether the new array contains same values as the previous one.

  • 0

    In this problem, you do return the length of the new array, but that is because you are editing the one passed in. Arrays are passed in by reference, so by changing it inside your method, you change the one outside the method as well. You return the new length so that the tester knows where to stop while traversing the array, since arr.length is no longer valid.

    Since it is saying "Output: [2] Expected: [1,2]", it is likely that you eliminated the 1 and returned 1 as the new length for this test.

Log in to reply

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