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 ;
}
}
该博客讨论了一个编程问题,即如何在不使用额外空间的情况下,原地修改已排序的整数数组,删除重复的元素并返回新数组的长度。提供的解决方案中,通过遍历数组并跳过连续重复的元素,实现了这一目标。
1095

被折叠的 条评论
为什么被折叠?



