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.

Example 1:

Input: [3,2,3]
Output: 3

Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2
class Solution {
public int majorityElement(int[] nums) {
int tmp = nums[0];
int count = 0;
for (int num : nums) {
if (count == 0) {
tmp = num;
} if (num == tmp) {
count += 1;
} else {
count -= 1;
}
}
return tmp;
}
}

最新文章

  1. Bootstrap框架的学习(一)
  2. JavaScript作用域链
  3. python 中的decorator
  4. [原创]NDT方法在SLAM中的应用
  5. JavaScript中Element与Node的区别,children与childNodes的区别
  6. linux系统日志及其rsyslog服务
  7. IDL 实现PCA算法
  8. java中把list列表转为arrayList以及arraylist数组截取的简单方法
  9. wpf之StackPanel、WrapPanel、WrapPanel之间的关系
  10. IOS开发之XCode学习014:警告对话框和等待提示器
  11. ssh三大框架集成后,jsp中采用forword标签提交时会报错的解决方案
  12. ndk编译ffmpeg
  13. Python 3 iter函数用法简述
  14. LodopJS代码模版的加载和赋值
  15. C 语言的关键字static 和C++ 的关键字static 有什么区别
  16. VC++ 常见问题及其解决方法
  17. iOS蓝牙空中升级(固件升级)
  18. Ansible 书写我的playbook
  19. 解决NSTextContainer分页时文本截断问题
  20. Docker入门与应用系列(二)镜像管理

热门文章

  1. alert(1&&2)的输出问题
  2. Dijkstra与Floyd算法
  3. ELK简单配置
  4. python 网络请求、下载
  5. 题解 P2831 【愤怒的小鸟】
  6. 第一个struts2框架
  7. spring boot集成MyBatis 通用Mapper 使用总结
  8. Tkinter控件
  9. WEB网站的favicon.ico的设置
  10. ansible puppet saltstack三款自动化运维工具的对比