Share My Simple Java Solution (very simple~_~)


  • 0
    L
    public static int removeDuplicates(int[] nums) 
    {
    	if (nums == null)
    		return 0;
    	int j = 0;
    	int len = nums.length;
    	int count = 1;
    	for (int i = 1; i < nums.length; i++) {
    		if (nums[i] == nums[j] && count != 0) {
    			j++;
    			count -= 1;
    			nums[j] = nums[i];
    			continue;
    		} else if (nums[i] == nums[j] && count == 0) {
    			len--;
    			continue;
    		} else if(nums[i] != nums[j]) {
                count = 1; 
    			j++;
                  nums[j] = nums[i];
    			
    		}
    	}
    	
    	return len;
    }

  • 0

    Even though this code produces the correct answer, I am a little concerned about that you use "count = 0" to indicate the current element have appear more than two times. Would it be more readability if you simply track the # of occurrence via the count variable and use the condition "count > 2".


  • 0
    L

    Thanks !Are you a chinese?


  • 0

    No problem. :) Yes, I am.


  • 0
    L

    。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。


Log in to reply
 

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