问题链接:LeetCode26. Remove Duplicates from Sorted Array
注意点:
1.数组中可能是0个元素;
2.C++程序中,循环变量声明不能写在for语句中(编译错误),只能写在外面(郁闷)。
AC的C语言程序如下:
int removeDuplicates(int* nums, int numsSize) { int count=1, *currnum = nums; if(numsSize == 0) return numsSize; while(--numsSize) if(*(++nums) != *currnum) { count++; *(++currnum) = *(nums); } return count; }AC的C++语言程序如下:
class Solution { public: int removeDuplicates(vector<int>& nums) { int count = nums.end() - nums.begin(); if(count == 0) return count; int i, j; for(i=0, j=1; j<count; j++) if(nums[j] != nums[i]) nums[++i] = nums[j]; return i+1; } };