Showing as wrong answer in OJ but is running fine in another compiler


  • 0
    P
    class Solution {
        
        int i,j;
    public:
        int removeDuplicates(int A[], int n) {
            
          
            for (i=0;i<n;i++)
            {
                for(j=i+1;j<=n;j++)
                {
                    if(A[i] == A[j])
                    {
                        A[j] = A[j+1];
                        n--;
                    }
                }
            }
            n++;
            
            return n;
        }
    };
    

    OJ is giving this soln. as wrong answer

    Input: []

    Output: [0]

    Expected: []

    though the same soln. is working in a different compiler


  • 0
    S

    Thanks for your post. I just updated your post with correct code format, hope you will pay attention next time.


  • 2
    S

    This test case means, giving you an empty list, n=0. The goals of this problem is to remove duplicated elements in list. For this test case should return 0, but in your code, finally return 1. It must be wrong.


  • 0
    S

    This just be created by over flow! "A[j] = A[j+1];" and you set "j" as "j<=n", and when "j=n" and what is the result of A[j+1] ?


  • 0
    P

    thnx I m slowly getting hold of its return codes


Log in to reply
 

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