Majority Element II
2015-09-12 02:45
190 查看
Given an integer array of size n, find all elements that appear more than
Analyse: map.
Runtime: 40ms.
⌊ n/3 ⌋times. The algorithm should run in linear time and in O(1) space.
Analyse: map.
Runtime: 40ms.
class Solution { public: vector<int> majorityElement(vector<int>& nums) { int n = nums.size(); map<int, int> m; vector<int> result; for(int i = 0; i < n; i++){ if(m.find(nums[i]) != m.end()) m[nums[i]]++; else m[nums[i]] = 1; } for(map<int, int>::iterator ite = m.begin(); ite != m.end(); ite++){ if(ite->second > n / 3) result.push_back(ite->first); } return result; } };
相关文章推荐
- 计票统计
- 设计模式交流会总结
- Zabbix 监控 Nginx
- Flash Stage3D 在2D UI 界面上显示3D模型问题完美解决
- POJ 2155 解题报告
- Effective C++ 条款46 需要类型转换时请为模板定义非成员函数
- Codeforces Round #319 (Div. 2)(A,B,C,E)
- 使用ngx_lua构建高并发应用
- HYSBZ 1492 Cash(CDQ分治论文题)
- mongodb副本集群搭建
- 手机U盘制作成系统启动盘后在手机端无法识别
- Achive后 没有ios apps 只有other items
- spring,cxf,restful发布webservice传递List,Map,List<Map>
- CentOS 开放 80、8080 端口
- eclipse的svn插件出现问题,64位系统,解决方法
- 基础知识快速提升
- android-整体UI设计-(滑动导航栏+滚动页面)
- Unity3D简单电影视图编辑
- 苹果Mac隐藏壁纸在哪里?Mac隐藏壁纸查找教程
- java实现excel的导入导出(poi详解)