8000 Fixed remove-duplicates-from-sorted-array · call-fold/leetcode@cca835d · GitHub
[go: up one dir, main page]

Skip to content

Commit cca835d

Browse files
committed
Fixed remove-duplicates-from-sorted-array
1 parent bbea1b3 commit cca835d

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

C++/chapLinearList.tex

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ \subsubsection{代码1}
2929
// 时间复杂度O(n),空间复杂度O(1)
3030
class Solution {
3131
public:
32-
int removeDuplicates(int A[], int n) {
33-
if (n == 0) return 0;
32+
int removeDuplicates(vector<int>& nums) {
33+
if (nums.empty()) return 0;
3434

3535
int index = 0;
36-
for (int i = 1; i < n; i++) {
37-
if (A[index] != A[i])
38-
A[++index] = A[i];
36+
for (int i = 1; i < nums.size(); i++) {
37+
if (nums[index] != nums[i])
38+
nums[++index] = nums[i];
3939
}
4040
return index + 1;
4141
}
@@ -49,8 +49,8 @@ \subsubsection{代码2}
4949
// 使用STL,时间复杂度O(n),空间复杂度O(1)
5050
class Solution {
5151
public:
52-
int removeDuplicates(int A[], int n) {
53-
return distance(A, unique(A, A + n));
52+
int removeDuplicates(vector<int>& nums) {
53+
return distance(nums.begin(), unique(nums.begin(), nums.end()));
5454
}
5555
};
5656
\end{Code}
@@ -62,8 +62,8 @@ \subsubsection{代码3}
6262
// 使用STL,时间复杂度O(n),空间复杂度O(1)
6363
class Solution {
6464
public:
65-
int removeDuplicates(int A[], int n) {
66-
return removeDuplicates(A, A + n, A) - A;
65+
int removeDuplicates(vector<int>& nums) {
66+
return distance(nums.begin(), removeDuplicates(nums.begin(), nums.end(), nums.begin()));
6767
}
6868

6969
template<typename InIt, typename OutIt>

0 commit comments

Comments
 (0)
0