Created
July 18, 2016 07:17
-
-
Save jianminchen/7406702476e8fe7b58934fccb9a4e674 to your computer and use it in GitHub Desktop.
Majority - facebook code lab - study the code provided by facebook code lab.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int majorityElement(vector<int> &num) { | |
int majorityIndex = 0; | |
for (int count = 1, i = 1; i < num.size(); i++) { | |
num[majorityIndex] == num[i] ? count++ : count--; | |
if (count == 0) { | |
majorityIndex = i; | |
count = 1; | |
} | |
} | |
return num[majorityIndex]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment