I am ashamed about my codes. It can work, but not general.


  • 0
    C
    	public static int removeDuplicates(int[] nums) {
    		int count = 1;
    		int k = 0;
    		boolean change = false;
    		for(int i = 1; i<nums.length;i++) {
    			if(nums[i] == nums[i-1]) {
    				if(!change){
    					k = i;
    					change = true;
    				}
    			
    			}
    			else{
    				if(change){
    					nums[k] = nums[i];
    					k++;
    				}
    				count++;
    			}
    		}
    		return count;
    	}

Log in to reply
 

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