169. Majority Element
2016-03-02 17:09
267 查看
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.
解题思路:
思路请看剑指offer的面试29.。。。。。
You may assume that the array is non-empty and the majority element always exist in the array.
解题思路:
思路请看剑指offer的面试29.。。。。。
class Solution { public: int majorityElement(vector<int>& nums) { int n=nums.size(); if(n==1)return nums[0]; int pre=nums[0]; int cnt=1; for(int i=0;i<n;i++){ if(nums[i]==pre)cnt++; else cnt--; if(cnt==0){ pre=nums[i]; cnt=1; } } return pre; } }; |
相关文章推荐
- 268. Missing Number
- 视图View,获取视图大小
- 分辨率验证工具 - 【Window Resizer】的使用 - Google扩展工具
- 个人项目详述
- 性能测试培训:通用互联网服务端程序
- 怎样提交表单但是不刷新页面
- 【mergeSort】
- Python-----sorted
- 如何设置mac虚拟机中的语言
- java5中的 Map 与线程安全
- MapGIS6.7_学习中遇到的问题记录下来(1)
- 性能测试培训:Ajax接口级性能测试之jmeter版
- [bzoj2324][ZJOI2011]营救皮卡丘
- 基于Nginx服务器和iOS9的HTTPS安全通信
- windows下安装redis
- poj--3164(次小生成树)
- 性能测试(二):性能测试的分析步骤
- C++调用lib或dll文件
- 【电脑关机后,电源指示灯仍然亮着!】
- js 搜索关键字,文章中关键字高亮显示