Share my code, someone can do to improve my algorithm?


  • 0
    Z

    public int removeDuplicates(int[] nums){

        	if(nums.length<=2)
        		return nums.length;
        	int i=0,j=1,times=1;
        	for(;j<nums.length;j++){
        		if(nums[j]!=nums[i]){
        			nums[++i]=nums[j];
        			times=1;
        		}else if(times<2){
        			nums[++i]=nums[j];
        			times++;
        		}else{
        			times++;
        		}
        	}
    	    return i+1;        
    

    }


Log in to reply
 

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