# 一.问题描述

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.

# 二.我的解题思路

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int len = nums.size();int cnt=1;int st=0;
if(len<2) return len;
for(int i=1;i<len;i++){
if(nums[i]!=nums[i-1]){
cnt++;
if(st<i-1){
nums[st+1]=nums[i];

}
st++;
}

}
return cnt;
}
};

• 本文已收录于以下专栏：

## 【LeetCode】Remove Duplicates from Sorted Array 解题报告

【LeetCode】Remove Duplicates from Sorted Array 解题报告标签（空格分隔）： LeetCode[LeetCode]https://leetcode.com/p...

## LeetCode_Easy心得：26. Remove Duplicates from Sorted Array（C语言）

26. Remove Duplicates from Sorted Array

## LeetCode Remove Duplicates from Sorted Array 移除有序数列的重复元素

Given a sorted array, remove the duplicates in place such that each element appear only once and ret...

## leetcode：数组：Remove Duplicates from Sorted Array

leetcode：数组之Remove Duplicates from Sorted Array 去除有序数组中重复的元素，例如a=[1,1,2,3];输出a=[1,2,3],返回数组长度。 c++实现...

## LeetCode | Remove Duplicates from Sorted Array II（删除重复的元素2）

Remove Duplicates from Sorted Array II

## LeetCode | Remove Duplicates from Sorted Array

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)