Skip to content

Commit d1a213f

Browse files
committed
增加了 hansonyu 贡献的一份代码
1 parent d5605f6 commit d1a213f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

C++/LeetCodet题解(C++版).pdf

849 Bytes
Binary file not shown.

C++/chapLinearList.tex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ \subsubsection{代码}
115115
};
116116
\end{Code}
117117

118+
下面是一个更简洁的版本。上面的代码略长,不过扩展性好一些,例如将\fn{occur < 2}改为\fn{occur < 3},就变成了允许重复最多3次。
119+
\begin{Code}
120+
// LeetCode, Remove Duplicates from Sorted Array II
121+
// @author hansonyu (http://weibo.com/u/1666779725)
122+
class Solution {
123+
public:
124+
int removeDuplicates(int A[], int n) {
125+
int index = 0;
126+
for (int i = 0; i < n; ++i) {
127+
if (i > 0 && i < n - 1 && A[i] == A[i - 1] && A[i] == A[i + 1])
128+
continue;
129+
130+
A[index++] = A[i];
131+
}
132+
return index;
133+
}
134+
};
135+
\end{Code}
136+
118137

119138
\subsubsection{相关题目}
120139

0 commit comments

Comments
 (0)