-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path169.cpp
49 lines (42 loc) · 908 Bytes
/
169.cpp
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
#include <cassert>
#include <gtest/gtest.h>
#include <vector>
using namespace std;
class Solution {
public:
int majorityElement(vector<int> &nums)
{
int count = 0;
int candidate = 0;
for (int num : nums) {
if (count == 0) {
candidate = num;
}
if (num == candidate) {
count++;
}
else {
count--;
}
}
return candidate;
}
};
class Testing : public testing::Test {
public:
Solution solution;
void SetUp() {}
void TearDown() {}
};
TEST_F(Testing, Case1)
{
vector<int> nums = {3, 2, 3};
int expected = 3;
EXPECT_EQ(solution.majorityElement(nums), expected);
}
TEST_F(Testing, Case2)
{
vector<int> nums = {2, 2, 1, 1, 1, 2, 2};
int expected = 2;
EXPECT_EQ(solution.majorityElement(nums), expected);
}