当前位置:网站首页>剑指 Offer 39. 数组中出现次数超过一半的数字
剑指 Offer 39. 数组中出现次数超过一半的数字
2022-06-25 16:34:00 【grt要一直一直努力呀】

class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}
利用此种方法是可以通过的,但显然Arrays.sort不是本题考察的重点。
摩尔投票法
class Solution {
public int majorityElement(int[] nums) {
int x = 0, votes = 0;
for(int num : nums){
if(votes == 0) x = num;
votes += num == x ? 1 : -1;
}
return x;
}
}
边栏推荐
- 心楼:华为运动健康的七年筑造之旅
- Day_ ten
- ncnn源码学习全集
- Cache architecture scheme of ten million level shopping cart system
- Multiple decorators decorate a function
- 使用hbuilder X创建uniapp项目
- Xinlou: Huawei's seven-year building journey of sports health
- JVM memory structure
- Understanding of reflection part
- This latest research has revealed two secrets of cloudy development
猜你喜欢

Kalman Filter 遇到 Deep Learning : 卡尔曼滤波和深度学习有关的论文

How did I raise my salary to 20k in three years?

Read mysql45 - a simple understanding of global locks and table locks

2022-06-17 网工进阶(九)IS-IS-原理、NSAP、NET、区域划分、网络类型、开销值

Reading mysql45 lecture - index continued

解析数仓lazyagg查询重写优化

Day_ 18 hash table, generic

Bombard the headquarters. Don't let a UI framework destroy you

Navicat premium 15 for MAC (database development tool) Chinese version

深入理解和把握数字经济的基本特征
随机推荐
Uniapp to preview pictures (single / multiple)
Unity技术手册 - 生命周期内大小(Size over Lifetime)和速度决定大小(Size by Speed)
万卷书 - 大力娃的书单
_ 17 collection overview
从TiDB上线阿里云的背后,如何看待云数据库的变革趋势
tensorflow 旧版本
Bombard the headquarters. Don't let a UI framework destroy you
Swift responsive programming
What is backbone network
协议和分层次
Day_ 04
Day_ 18 hash table, generic
心楼:华为运动健康的七年筑造之旅
XXIX - orbslam2 real-time 3D reconstruction using realsensed435
Ad domain login authentication
Final, override, polymorphic, abstract, interface
Generate post order traversal according to pre order traversal and mid order traversal
千万级购物车系统缓存架构方案
Record learning of hystrix knowledge --20210929
MySQL_ JDBC