Created
September 14, 2015 14:02
-
-
Save chenyanzhe/800659aa716ebafbcf08 to your computer and use it in GitHub Desktop.
[腾讯笔试] 2016腾讯校招技术类笔试(大题部分) 第四题
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
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int find(vector<int> nums) { | |
if (nums.size() == 0) | |
return -1; | |
int value = nums[0]; | |
int count = 1; | |
for (int i = 2; i < nums.size(); i++) { | |
if (nums[i] == value) | |
count++; | |
else { | |
count--; | |
if (count < 0) { | |
value = nums[i]; | |
count = 1; | |
} | |
} | |
} | |
if (count > 0) | |
return value; | |
else | |
return -1; | |
} | |
int main() { | |
vector<int> nums {1, 1, 1, 1, 1, 1, 2, 3, 4, 5}; | |
cout << find(nums) << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment