Leetcode#169 Majority Element

原题地址

寻找主元素

非常巧妙的方法

代码:

 1 int majorityElement(vector<int> &num) {
 2   int candidate = 0;
 3   int count = 0;
 4         
 5   for (int i = 0; i < num.size(); i++) {
 6     if (count == 0) {
 7       candidate = num[i];
 8       count = 1;
 9     }
10     else
11       count += candidate == num[i] ? 1 : -1;
12   }
13         
14   return candidate;
15 }
原文地址:https://www.cnblogs.com/boring09/p/4251281.html