Majority Element
来源:互联网 发布:少女大召唤知轩藏书 编辑:程序博客网 时间:2024/03/29 23:32
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
class Solution {public: int majorityElement(vector<int> &num) { int len = num.size(); int tmp=num[0], nb=1; for(int i=1;i<len;i++) { if(num[i]!=tmp) nb--; else nb++; if(nb==0) { tmp = num[i]; nb = 1; } } return tmp; }};
0 0
- majority element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- majority element
- Majority Element
- Majority Element
- 内存分配函数 _alloc/malloc/calloc
- 发表测试123
- iOS——day1-day6笔记
- PCL之空间非合作目标位姿估计
- Android笔记---- Android 应用程序设计基础
- Majority Element
- 性能测试用例模板及相关
- Mysql数据库的索引原理
- 数组变量与算术运算
- linux命令之grep
- C# 用户自定义UserControl控件使用
- HDU 2527 Safe Or Unsafe 【Huffman编码】
- 解释下浏览器是如何判断元素是否匹配某个 CSS 选择器?
- 斯坦福大学公开课 :机器学习课程(Andrew Ng)——5、监督学习:Support Vector Machine,引