We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent cd2e65f commit 5a0d167Copy full SHA for 5a0d167
problems/80.remove-duplicates-from-sorted-array-ii.md
@@ -106,19 +106,25 @@ CPP Code:
106
```cpp
107
class Solution {
108
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];
+ int removeDuplicates(vector<int>& nums) {
+ int i = 0;
+ int k = 2;
+ for (int num : nums) {
113
+ if (i < k || num != nums[i - k]) {
114
+ nums[i] = num;
115
+ i++;
116
+ }
117
}
- return j;
118
+ return i;
119
120
};
121
```
122
123
**复杂度分析**
124
-- 时间复杂度:$O(N)$
125
+令 n 为数组长度。
126
+
127
+- 时间复杂度:$O(n)$
128
- 空间复杂度:$O(1)$
129
130
基于这套代码,你可以轻易地实现 k 为任意正整数的算法。
0 commit comments