您的位置:首页 > 其它

26. Remove Duplicates from Sorted Array

2017-02-27 11:56 393 查看
简单题,关键在于细节处理。

class Solution {
public:
int removeDuplicates(vector<int>& nums) {

if(nums.size()==0)
return 0;

int preIndex=0;
int nowIndex=0;
while(nowIndex<nums.size())
{
if(nums[preIndex]>=nums[nowIndex])
nowIndex++;
else
{

if(nowIndex-preIndex!=1)
{
int temp=nums[preIndex+1];
nums[preIndex+1]=nums[nowIndex];
nums[nowIndex]=temp;
}
preIndex++;
nowIndex++;
}
}

return preIndex+1;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: