class Solution {
public:
int removeDuplicates(vector<int>& nums) {
// 核心思想很简单,一个是确定位置的指针index,每当nums[index]的值和nums[i]不同的时候,nums[index]的值就要被nums[i]覆盖,就是这么简单
if (nums.empty())
return 0;
int index = 0;
int count = 1;
unsigned int n = nums.size();
for (int i = 1; i < n; i++) {
if (nums[index] != nums[i]) {
nums[++index] = nums[i];
count += 1;
}
}
return count;
}
}
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty())
return 0;
int index = 0;
int count = 1;
unsigned int n = nums.size();
for (int i = 1; i < n; i++) {
if (nums[index] == nums[i]) {
i++; // 非常严重的错误,在for循环中对i进行自加一操作 破坏了原来的流程 //破坏了原来的流程,step == 2
}
else { //其实只需要判断不等的情况就够了
nums[++index] = nums[i];
count += 1;
}
}
for (int i = 0; i < nums.size(); i++) {
cout << nums[i] << endl;
}
return count;
}
}
class Solution {public: int removeDuplicates(vector&lt;int&gt;&amp; nums) {// 核心思想很简单,一个是确定位置的指针index,每当nums[index]的值和nums[i]不同的时候,nums[index]的值就要被nums[i]覆盖,就是这么简单 if (nums.empty()) return 0; ...