面试题39. 数组中出现次数超过一半的数字
2021-01-26 04:13
标签:ret solution return 图片 alt inf 面试 http 超过一半 题目: 解答:
面试题39. 数组中出现次数超过一半的数字 标签:ret solution return 图片 alt inf 面试 http 超过一半 原文地址:https://www.cnblogs.com/ocpc/p/12858473.html 1 class Solution {
2 public:
3 int majorityElement(vectorint>& nums)
4 {
5 int x = 0;
6 int votes = 0;
7 for(int num : nums)
8 {
9 if(votes == 0)
10 {
11 x = num;
12 }
13 if (num == x)
14 {
15 votes += 1;
16 }
17 else
18 {
19 votes -= 1;
20 }
21 // votes += num == x ? 1 : -1;
22 }
23 return x;
24 }
25 };
文章标题:面试题39. 数组中出现次数超过一半的数字
文章链接:http://soscw.com/index.php/essay/47100.html