5.2
题目要求是不许使用额外的空间。
所以这样写就是错误的了 :nums = Arrays.copyOf(nums, nums.length-1); 这是建立了新的数组。
直接标记length 的长度就可以了。
这个题是不需要重新维护数组的。
public class Solution {
/**
* @param A: a array of integers
* @return : return an integer
*/
public int removeDuplicates(int[] nums) {
// write your code here
int length = nums.length;
if(length <= 1){
return length;
}
int i = 0;
for(i = 0; i < length - 1; i++){
while(i < length -1 && nums[i] == nums[i+1]){
for(int j = i+1;j < length-1;j++){
nums[j] = nums[j+1];
}
length --;
//nums = Arrays.copyOf(nums, nums.length-1);
//System.out.println(Arrays.toString(nums));
}
}
return length ;
}
}