Skip to content

Instantly share code, notes, and snippets.

@chenyanzhe
Created September 14, 2015 14:02
Show Gist options
  • Save chenyanzhe/800659aa716ebafbcf08 to your computer and use it in GitHub Desktop.
Save chenyanzhe/800659aa716ebafbcf08 to your computer and use it in GitHub Desktop.
[腾讯笔试] 2016腾讯校招技术类笔试(大题部分) 第四题
#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