Skip to content

Commit bdd2a5f

Browse files
authored
Create solution.cpp
1 parent 0923863 commit bdd2a5f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

169. majority_element/solution.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution
2+
{
3+
public:
4+
int majorityElement(vector<int>& nums)
5+
{
6+
int num;
7+
int cnt = 0;
8+
for (int i = 0;i < nums.size();i++)
9+
{
10+
if (cnt == 0)
11+
{
12+
num = nums[i];
13+
cnt++;
14+
}
15+
else
16+
{
17+
cnt += (num == nums[i]) ? 1 : -1;
18+
}
19+
}
20+
return num;
21+
}
22+
};

0 commit comments

Comments
 (0)