当前位置:网站首页>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;
}
边栏推荐
- The data read by pandas is saved to the MySQL database
- The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
- 2021-10-28
- 01 business structure of imitation station B project
- CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
- CV learning notes - image filter
- My 4G smart charging pile gateway design and development related articles
- 01仿B站项目业务架构
- ADS simulation design of class AB RF power amplifier
- JS foundation - prototype prototype chain and macro task / micro task / event mechanism
猜你喜欢

Swing transformer details-2

LeetCode - 919. 完全二叉树插入器 (数组)

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

03 FastJson 解决循环引用

LeetCode - 900. RLE 迭代器

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

LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *

LeetCode - 919. Full binary tree inserter (array)
![[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 300 最长上升子序列
随机推荐
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
CV learning notes - reasoning and training
Not many people can finally bring their interests to college graduation
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
(2) New methods in the interface
The underlying principle of vector
Opencv gray histogram, histogram specification
4G module IMEI of charging pile design
Do you understand automatic packing and unpacking? What is the principle?
MySQL root user needs sudo login
Leetcode 300 longest ascending subsequence
Dictionary tree prefix tree trie
Positive and negative sample division and architecture understanding in image classification and target detection
pycharm 无法引入自定义包
Serial communication based on 51 single chip microcomputer
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Opencv notes 17 template matching
Qcombox style settings
LeetCode - 5 最长回文子串