标签:
1 class Solution { 2 public: 3 int MoreThanHalfNum_Solution(vector<int> numbers) { 4 if (numbers.size() == 0) 5 return 0; 6 int result = numbers[0]; 7 int times = 1, count = 0; 8 for (int i = 1; i < numbers.size(); i++){ 9 if (times == 0){ 10 result = numbers[i]; 11 times = 1; 12 } 13 else if (numbers[i] == result) 14 times++; 15 else 16 times--; 17 } 18 for (int i = 1; i < numbers.size(); i++){ 19 if (numbers[i] == result){ 20 count++; 21 } 22 } 23 if (count >= numbers.size() / 2) 24 return result; 25 else 26 return 0; 27 28 } 29 };
标签:
原文地址:http://www.cnblogs.com/wanderingzj/p/5357460.html