My JAVA Solution


  • 0
    Q
    public class Solution {
        public int removeDuplicates(int[] nums) {
            if (nums.length<=1){
                return nums.length;
            }
            int count = 1;
            for (int i=1,pointer=1;i<nums.length;i++,pointer++){
            	if (nums[i] <= nums[i-1]){
    	        	while (pointer<nums.length  && (nums[i] >= nums[pointer] || (nums[i-1]>= nums[pointer]))){
    	        		pointer++;
    	        	}
            	}
            	if (pointer<nums.length){
            		nums[i] = nums[pointer];
            		if (nums[i]!=nums[i-1]){
            			count++;
            		}
            		else{
            		    break;
            		}
            	}
            }
            return count;
        }
    }

Log in to reply
 

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