File tree Expand file tree Collapse file tree 1 file changed +9
-16
lines changed Expand file tree Collapse file tree 1 file changed +9
-16
lines changed Original file line number Diff line number Diff line change @@ -112,22 +112,15 @@ \subsubsection{代码1}
112
112
class Solution {
113
113
public:
114
114
int removeDuplicates(int A[], int n) {
115
- if (n == 0) return 0;
116
-
117
- int occur = 1;
118
- int index = 0;
119
- for (int i = 1; i < n; i++) {
120
- if (A[index] == A[i]) {
121
- if (occur < 2) {
122
- A[++index] = A[i];
123
- occur++;
124
- }
125
- } else {
126
- A[++index] = A[i];
127
- occur = 1;
128
- }
115
+ if(n <= 2) return n;
116
+
117
+ int index = 2;
118
+ for(int i = 2; i < n; i ++){
119
+ if(A[i] != A[index - 2])
120
+ A[index ++] = A[i];
129
121
}
130
- return index + 1;
122
+
123
+ return index;
131
124
}
132
125
};
133
126
\end {Code }
@@ -241,7 +234,7 @@ \subsubsection{分析}
241
234
\subsubsection {代码 }
242
235
\begin {Code }
243
236
// LeetCode, Search in Rotated Sorted Array II
244
- // 时间复杂度O(log n),空间复杂度O(1)
237
+ // 时间复杂度O(n),空间复杂度O(1)
245
238
class Solution {
246
239
public:
247
240
bool search(int A[], int n, int target) {
You can’t perform that action at this time.
0 commit comments