Last active
August 29, 2015 14:22
-
-
Save cangoal/7656195af94bbb6c0867 to your computer and use it in GitHub Desktop.
LeetCode - Remove Duplicates from Sorted Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
public int removeDuplicates(int[] nums) { | |
if(nums == null) return 0; | |
if(nums.length < 2) return nums.length; | |
int k = 0, p = 1; | |
while(p < nums.length){ | |
if(nums[k] != nums[p]){ | |
nums[++k] = nums[p]; | |
} | |
p++; | |
} | |
return k+1; | |
} | |
// | |
public int removeDuplicates(int[] nums) { | |
if(nums == null) return 0; | |
if(nums.length < 2) return nums.length; | |
int k=1; | |
for(int i=1; i < nums.length; i++){ | |
if(nums[k-1] != nums[i]){ | |
nums[k++] = nums[i]; | |
} | |
} | |
return k; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment