当前位置:网站首页>剑指 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;
}
}
边栏推荐
- DDD概念复杂难懂,实际落地如何设计代码实现模型?
- Uniapp to preview pictures (single / multiple)
- Unity技术手册 - 干扰/噪音/杂波(Noise)子模块
- 千万级购物车系统缓存架构方案
- Wireshark network card cannot be found or does not display the problem
- Reverse series to obtain any wechat applet code
- [Jianzhi offer II 091. painting the house]
- WPF development essays Collection - ECG curve drawing
- Precautions for function default parameters (formal parameter angle)
- 【剑指 Offer II 091. 粉刷房子】
猜你喜欢
随机推荐
万卷书 - 大力娃的书单
Mac PHP multi version management and swoole extension installation
解析数仓lazyagg查询重写优化
Day_ ten
2022-06-17 网工进阶(九)IS-IS-原理、NSAP、NET、区域划分、网络类型、开销值
软件测试面试如何正确谈薪
What is backbone network
User login 2
Read mysql45 lecture - index
IO stream
论文笔记:Generalized Random Forests
JVM内存结构
Hash table, generic
知道这些面试技巧,让你的测试求职少走弯路
【精通高并发】深入理解汇编语言基础
Common APIs and exception mechanisms
User registration, information writing to file
Simple dialogue system -- implement transformer by yourself
flutter
批量--07---断点重提








