leetcode刷题,总结,记录,备忘 169
2015-06-21 23:47
387 查看
leetcode169Majority Element
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.
这个题目很简单,,,排序一下,遍历统计一下最出现次数最多,保存下来,即可。
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) { sort(nums.begin(), nums.end()); int maxv = nums[0], maxvc = nums[0], maxc = 0, count = 0; for (vector<int>::iterator it = nums.begin(); it != nums.end(); ++it) { if (maxvc != *it) { if (count > maxc) { maxv = maxvc; maxc = count; } maxvc = *it; count = 0; } count++; } if (count > maxc) { maxv = maxvc; maxc = count; } return maxv; } };
相关文章推荐
- ASP.NET 学习博客
- 双系统安装
- 7.MyBatis 关联查询(一对一)
- Excel随机生成数据
- Django开发BBS---51网络课程笔记(2)
- Redis持久化
- java基础—采用递归的方式来获取相关目录下的子目录的名称
- JavaScript字符串函数大全
- 《人,绩效和职业道德》读后感
- linux安装JDK笔记
- 2015062109 - 坚持
- 用js获取当前页面的url的相关信息方法
- Matlab 为什么要使用面向对象的编程——开篇
- 拥抱 Gradle: 下一代自动化工具
- MySQL 和 c++ 连接时报错error C2146: syntax error : missing ';' before identifier 'fd'
- 14.命令模式
- 团队项目---测试与调试
- CoreSpotlight简单入门
- 《人,绩效和职业道德》读后感
- 对其他组评价的反馈