当前位置:网站首页>20220609其他:多数元素
20220609其他:多数元素
2022-07-03 09:20:00 【丿SeeYouAgain】
题目描述:给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。你可以假设数组是非空的,并且给定的数组总是存在多数元素。
编码实现:
public int majorityElement(int[] nums) {
int count = 1,result = nums[0];
for (int i = 1; i < nums.length; i++) {
if (result == nums[i]){
count++;
} else {
count--;
if (count == 0) {
result = nums[i + 1];
}
}
}
return result;
}
边栏推荐
- Opencv notes 17 template matching
- 51 MCU tmod and timer configuration
- RESNET code details
- Pymssql controls SQL for Chinese queries
- Google browser plug-in recommendation
- Connect Alibaba cloud servers in the form of key pairs
- is_ power_ of_ 2 judge whether it is a multiple of 2
- Circular queue related design and implementation reference 1
- Vgg16 migration learning source code
- Wireshark use
猜你喜欢

ADS simulation design of class AB RF power amplifier

Swing transformer details-1

Octave instructions

LeetCode - 673. Number of longest increasing subsequences

I think all friends should know that the basic law of learning is: from easy to difficult

The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving

Exception handling of arm

CV learning notes - camera model (Euclidean transformation and affine transformation)

Opencv feature extraction sift

QT self drawing button with bubbles
随机推荐
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
Synchronization control between tasks
01仿B站项目业务架构
Google browser plug-in recommendation
Timer and counter of 51 single chip microcomputer
QT setting suspension button
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Vgg16 migration learning source code
Application of 51 single chip microcomputer timer
It is difficult to quantify the extent to which a single-chip computer can find a job
byte alignment
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
2021-10-28
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
Opencv feature extraction sift
pycharm 无法引入自定义包
LeetCode - 919. 完全二叉树插入器 (数组)
LeetCode - 673. 最长递增子序列的个数