26. Remove Duplicates from Sorted Array QuestionEditorial Solution My Submissions
Total Accepted: 166298
Total Submissions: 476995
Difficulty: Easy
Contributors: Admin
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.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
Total Accepted: 166298
Total Submissions: 476995
Difficulty: Easy
Contributors: Admin
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.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
Subscribe to see which companies asked this question
这段代码用了一个标志量来进行判断,看是否已经出现过,如果没有出现过,那么就加进数组之中
代码:
int removeDuplicates(int* nums, int numsSize) {
if(numsSize==0)
return 0;
int now=nums[0];
int sum=1;
for(int i=1;i<numsSize;i++){
if(nums[i]!=now){
now=nums[i];
nums[sum]=now;
sum++;
}
}
return sum;
}