-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_duplicates.cpp
More file actions
49 lines (45 loc) · 1.21 KB
/
remove_duplicates.cpp
File metadata and controls
49 lines (45 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* =====================================================================================
*
* Filename: remove_duplicates.cpp
*
* Description: 26. Remove Duplicates from Sorted Array
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/
*
* Version: 1.0
* Created: 03/08/23 10:18:51
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::vector;
// Two pointers, time O(N), space O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int j = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] != nums[j]) {
nums[++j] = nums[i];
}
}
return (j + 1);
}
};
TEST(Solution, removeDuplicates) {
vector<std::pair<vector<int>, int>> cases = {
std::make_pair(vector<int>{1, 1, 2}, 2),
std::make_pair(vector<int>{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}, 5),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().removeDuplicates(c.first), c.second);
}
}