Skip to content

Commit 5a0d167

Browse files
author
lucifer
committed
fix: cpp 代码和 python 保持一致
1 parent cd2e65f commit 5a0d167

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

problems/80.remove-duplicates-from-sorted-array-ii.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,25 @@ CPP Code:
106106
```cpp
107107
class Solution {
108108
public:
109-
int removeDuplicates(vector<int>& A) {
110-
int j = 0;
111-
for (int i = 0; i < A.size(); ++i) {
112-
if (j - 2 < 0 || A[j - 2] != A[i]) A[j++] = A[i];
109+
int removeDuplicates(vector<int>& nums) {
110+
int i = 0;
111+
int k = 2;
112+
for (int num : nums) {
113+
if (i < k || num != nums[i - k]) {
114+
nums[i] = num;
115+
i++;
116+
}
113117
}
114-
return j;
118+
return i;
115119
}
116120
};
117121
```
118122
119123
**复杂度分析**
120124
121-
- 时间复杂度:$O(N)$
125+
令 n 为数组长度。
126+
127+
- 时间复杂度:$O(n)$
122128
- 空间复杂度:$O(1)$
123129
124130
基于这套代码,你可以轻易地实现 k 为任意正整数的算法。

0 commit comments

Comments
 (0)