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