Remove Duplicates from Sorted Array删除排序数组中的重复数字
Description
Given a sorted array, ‘remove’ the duplicates in place such that each element appear only once and return the ‘new’ length.
Do not allocate extra space for another array, you must do this in place with constant memory.
public class Solution {
/*
* @param nums: An ineger array
* @return: An integer
*/
public int removeDuplicates(int[] nums) {
// write your code here
if(nums == null || nums.length == 0){
return 0 ;
}
int count = 0 ;
for(int i = 0 ; i < nums.length ; i++){
while(i < nums.length-1 && nums[i] == nums[i+1]){
i++ ;
}
nums[count] = nums[i] ;
count++ ;
}
return count ;
}
}