Run time error. Not sure why. It works on my local machine


  • 0
    M

    //cause runtime error.

    public class Solution {
        public int removeDuplicates(int[] A) {
           if(A==null)  return 0;
           if(A.length==1) return 1;
           int p1=0,p2=1,len=1;
           boolean isCopied=false;
           while(p2<A.length){
               if(A[p1]==A[p2]&&isCopied){
                   p2++;
               }else{
                    if(A[p1]==A[p2]){
                        isCopied=true;
                    }else{isCopied=false;}    
                   A[++p1]=A[p2++];
                   len++;
               }
           }
           return len;
        }
    }

  • 0
    K

    change to:

    public class Solution {
        public int removeDuplicates(int[] A) {
           if(A==null)  return 0;
           if(A.length==1) return 1;
           int p1=0,p2=1,len=1;
           boolean isCopied=false;
           while(p2<A.length){
               if(A[p1]==A[p2]&&isCopied){
                   p2++;
               }else{
                    if(A[p1]==A[p2]){
                        isCopied=true;
                    }else{
                       isCopied=false;   
                       A[++p1]=A[p2];
                       len++;
                   }
                   p2++;
               }
           }
           return len;
        }
    }
    

  • 0
    M

    It still has the same error


Log in to reply
 

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