leetcode-Majority Element
2015-11-06 20:32
155 查看
Difficulty:
Easy
Given an array of size n, find the majority element. The majority element is the element that appears more than
You may assume that the array is non-empty and the majority element always exist in the array.
Easy
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>& nums) { int candidate; int count=0; for(auto &e:nums){ if(count==0) candidate=e; if(e==candidate) ++count; else --count; } return candidate; } };
相关文章推荐
- swift 2.0语法 元组
- lintcode 中等题: Implement Trie
- 像素翻转、清除行列、彻底搞懂int *a[10]和int (*a)[10]
- v5
- 【DP】 HDOJ 3507 Print Article
- C++模板库(包含C++11特性)
- HDU 4612——Warm up——————【边双连通分量、树的直径】
- YOU健康小助手
- 剑指Offer——链表中快行指针用法(链表中倒数第k个结点等)
- JavaScript设计模式 Item 7 --策略模式Strategy
- [TwistedFate]category Extensxion protocol
- Linux shell(1)
- mysql事务
- JavaScript设计模式 Item 7 --策略模式Strategy
- HDU2087-剪花布条-KMP模板
- 简易 SolidWorks的三维飞行器模型建立教程
- 调整数组使奇数全部都位于偶数前面
- redis3.0集群状态监控脚本用于zabbix监控需要python2.6
- SpringMVC 学习系列 (1) 之 初识SpringMVC
- Android——网络编程