Majority Element
来源:互联网 发布:东方证券炒股软件 编辑:程序博客网 时间:2024/03/29 02:42
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.
if extend the problem to more than⌊ n/k ⌋ times,need a k-1 size array.
class Solution {public: int majorityElement(vector<int> &num){ if (num.empty()) return 0; int repeat = 1; int cur = num[0]; for (int i = 1; i < num.size(); i++) { if (cur == num[i]) repeat++; else if (--repeat == 0) { cur = num[i]; repeat = 1; } } return cur; }};
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
- SAP 系统的消息类型分析 MESSAGE TYPE
- android 添加随意拖动的桌面悬浮窗口(续)
- C# WPF 毫秒和时间转换类 源代码
- C#获取openfire的用户,包括增、删、改等
- Android Application解读
- Majority Element
- ios tableViewCell滑动实现多按钮
- struts2中property标签属性介绍
- Launcher源码学习 PageView中的ScrollIndicator分页指示器
- NSMutableString 使用(四)
- cocos2d-x-3.3rc2 通知中心
- Android关于context上下文定义全局的,转载一个大神的
- 12.2 掌握序列表达式
- Program Size: Code=29578 RO-data=4010 RW-data=488 ZI-data=18 衍生