当前位置:网站首页>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;
}
边栏推荐
- Swing transformer details-2
- Opencv histogram equalization
- Synchronization control between tasks
- LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
- Timer and counter of 51 single chip microcomputer
- Gpiof6, 7, 8 configuration
- 4G module IMEI of charging pile design
- The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
- [keil5 debugging] warning:enumerated type mixed with other type
- STM32 general timer output PWM control steering gear
猜你喜欢

LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)

LeetCode - 919. Full binary tree inserter (array)

Timer and counter of 51 single chip microcomputer
![[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer](/img/90/4de927e797ec9c2bb70e507392bed0.jpg)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer

LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))

2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)

Basic knowledge of communication interface

openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹

CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)

Interruption system of 51 single chip microcomputer
随机推荐
After clicking the Save button, you can only click it once
使用密钥对的形式连接阿里云服务器
2020-08-23
03 fastjason solves circular references
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
2021-11-11 standard thread library
01 business structure of imitation station B project
01仿B站项目业务架构
Stm32 NVIC interrupt priority management
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Opencv notes 20 PCA
yocto 技术分享第四期:自定义增加软件包支持
Windows下MySQL的安装和删除
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
Markdown latex full quantifier and existential quantifier (for all, existential)
My 4G smart charging pile gateway design and development related articles
自動裝箱與拆箱了解嗎?原理是什麼?
Screen display of charging pile design -- led driver ta6932
Blue Bridge Cup for migrant workers majoring in electronic information engineering
Gpiof6, 7, 8 configuration