题目描述
*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 A = [1,1,2], Your function should return length = 2, and A is now [1,2]. *my Code
/* * 对一个已经有序的序列进行重复元素删除 * 即保证序列中的元素是有序且唯一*/class Solution{public: int removeDuplicatesFromSortedArray(vector & nums) { //当序列为空时 if(nums.empty()) return 0; //当序列非空时 int index = 0; for(int i = 1;i
下面是测试代码:
#include#include using namespace std;/* * 对一个已经有序的序列进行重复元素删除 * 即保证序列中的元素是有序且唯一*/class Solution{public: int removeDuplicatesFromSortedArray(vector & nums) { //当序列为空时 if(nums.empty()) return 0; //当序列非空时 int index = 0; for(int i = 1;i