当前位置:网站首页>Sword finger offer 39 Numbers that occur more than half the time in the array
Sword finger offer 39 Numbers that occur more than half the time in the array
2022-06-25 16:59:00 【GRT has to keep working hard】

class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}
It is possible to use this method , But apparently Arrays.sort Is not the focus of this investigation .
Moore voting 
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;
}
}
边栏推荐
- Collection overview, array encapsulation
- Record learning of hystrix knowledge --20210929
- 这项最新的调查研究,揭开多云发展的两大秘密
- 揭秘GES超大规模图计算引擎HyG:图切分
- WPF development essays Collection - ECG curve drawing
- JVM内存结构
- 卡尔曼时间序列预测
- Read mysql45 lecture - index
- [100 questions of Blue Bridge Cup intensive training] scratch command mobile Blue Bridge Cup scratch competition special prediction programming question intensive training simulation exercise question
- pytorch官方文档学习记录
猜你喜欢
![[Jianzhi offer II 091. painting the house]](/img/63/dc54c411b1a2f2b1d69b62edafde38.png)
[Jianzhi offer II 091. painting the house]

【效率】又一款笔记神器开源了!

Record learning of hystrix knowledge --20210929

Wireshark network card cannot be found or does not display the problem

知道这些面试技巧,让你的测试求职少走弯路

剑指 Offer 39. 数组中出现次数超过一半的数字

Are these old system codes written by pigs?

Simple dialogue system -- implement transformer by yourself

Day_ eleven

使用PyWebIO测试,刚入门的测试员也能做出自己的测试工具
随机推荐
MySQL_ JDBC
Difference between app test and web test
Uncover ges super large scale graph computing engine hyg: Graph Segmentation
Kalman Filter 遇到 Deep Learning : 卡尔曼滤波和深度学习有关的论文
软件测试面试如何正确谈薪
Ncnn source code learning collection
Redis系列——概述day1-1
二十九-使用RealSenseD435进行ORBSLAM2实时三维重建
Read mysql45 the next day
Kalman filter meets deep learning: papers on Kalman filter and deep learning
File operation, serialization, recursive copy
The art of code annotation. Does excellent code really need no annotation?
The first day of reading mysql45
Mysql database multi table query
What is backbone network
Common APIs and exception mechanisms
A complete collection of APP testing tools. It's enough to collect this one
解析数仓lazyagg查询重写优化
这些老系统代码,是猪写的么?
Redis系列——概述day1-1