Skip to content

Commit 3068c0c

Browse files
committed
copy the thought from previous removeDuplicates.
1 parent 3b53a40 commit 3068c0c

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

C++/chapLinearList.tex

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,16 @@ \subsubsection{代码1}
112112
class Solution {
113113
public:
114114
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)
116+
return n;
117+
118+
int index = 2;
119+
for(int i = 2; i < n; i ++){
120+
if(A[i] != A[index - 2])
121+
A[index ++] = A[i];
129122
}
130-
return index + 1;
123+
124+
return index;
131125
}
132126
};
133127
\end{Code}

0 commit comments

Comments
 (0)