Use Two Points to solve this problem.


  • 0
    B

    public class Solution {
    public int removeDuplicates(int[] nums) {
    if (nums.length <= 1) return nums.length;

        int h1 = 0;
    	int h2 = -1;
    	int t = 1;
    	
    	while (t < nums.length) {
    		if (nums[h1] == nums[t] &&
    				h2 == -1){
    			h2 = h1;
    			nums[++h1] = nums[t];	
    		} else if (nums[h1] < nums[t]) {
    			nums[++h1] = nums[t];
    			h2 = -1;
    		}
    
    		t++;
    	}
    
        return h1+1;
    }
    

    }


Log in to reply
 

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