-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path169.cpp
More file actions
36 lines (33 loc) · 714 Bytes
/
169.cpp
File metadata and controls
36 lines (33 loc) · 714 Bytes
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
//
// 169.cpp
// LeetCode
//
// Created by 张佐玮 on 15/6/11.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Majority Element
//
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int majorityElement(vector<int>& nums) {
int start = 0, end = (int) nums.size() - 1, count = 0;
while (start < end) {
if (nums[start] != nums[end]) {
if (count) {
count--;
}
else {
start++;
}
}
else {
count++;
}
end--;
}
return nums[start];
}
};