Majority Element
2015-07-28 22:02
176 查看
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.
⌊ n/2 ⌋times.
You may assume that the array is non-empty and the majority element always exist in the array.
public class Solution { public int majorityElement(int[] nums) { int major = nums[0]; int count = 1; for( int i = 1; i < nums.length; i++){ if(count == 0){ count++; major = nums[i]; }else if(major == nums[i]){ count++; }else{ count--; } } return major; } }
相关文章推荐
- python基础补充
- 订单快递单号及查询快递进度
- C++ IO类
- css链接和内容
- Dump文件
- Nginx 笔记与总结(5)访问日志管理:计划任务 + 日志切割
- 自定义字体
- 在html借助元素特性存储信息
- UI005---延迟加载
- 天才的大三暑假修行之旅.捕鱼达人
- Python高效率编程的8条箴言
- 我遇见的关于android studio 导入studio项目常见问题
- 依据Uri获得sd卡图片
- 几种java通信(rmi,http,hessian,webservice)协议性能比较
- secureCRT连接后提示-bash: User: command not found的解决办法
- loadrunner 通过 cscript 获取js返回值的思路
- 黑马程序员——高新技术---Java基础-多线程1
- MySQL 1042错误 can‘t get hostname for your adress
- HDU Today
- 策进会四大佛教