Lintcode:主元素
来源:互联网 发布:ipad淘宝hd微淘在哪 编辑:程序博客网 时间:2024/05/21 07:43
给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一。
给出数组[1,1,1,1,2,2,2],返回 1
python:
class Solution: """ @param: nums: a list of integers @return: find a majority number """ def majorityNumber(self, nums): # write your code here ele = nums[0] count = 0 for i in nums: if ele == i: count += 1 else: count -= 1 if count < 0: ele = i return ele
C++:
class Solution {public: /* * @param nums: a list of integers * @return: find a majority number */ int majorityNumber(vector<int> &nums) { // write your code here int ele = nums[0]; int count = 0; for (int i = 0; i < nums.size(); i++) { if (ele == nums[i]) { count++; }else{ count--; if (count < 0) ele = nums[i]; } } return ele; }};
阅读全文
0 0
- Lintcode 主元素
- LintCode-主元素
- 主元素(LintCode)
- lintcode 主元素
- 主元素-----lintcode
- LintCode:主元素 III
- LintCode 主元素
- lintcode ----主元素
- LintCode(easy)主元素
- lintcode,主元素
- LintCode 主元素
- LintCode :主元素
- Lintcode 主元素 III
- LintCode 46 主元素
- [Lintcode]主元素
- Lintcode 主元素
- lintcode--主元素
- [LintCode]46.主元素
- 问题 Q: 恶魔的连营
- Ubuntu 下的搜狗输入法的安装
- Eclipse配置错误——An internal error occurred during: "Building workspace".GC overhead limit exceeded
- Shell变量及字符串的操作
- Tomcat服务器实现https
- Lintcode:主元素
- PAT 1090. Highest Price in Supply Chain (25) 从叶子节点出发去求树的高度
- 问题 R: 好看的Very Good
- 开发环境、生产环境、测试环境的基本理解和区别
- Two-Stream Convolutional Networks for Action Recognition in Videos算法笔记
- Python脚本报错AttributeError: ‘module’ object has no attribute’get’解决方法
- 拷贝构造函数的两种调用方式
- 清华集训2017滚粗记
- html和css再回首(2)