当前位置:网站首页>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;
}
边栏推荐
- LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
- LeetCode - 5 最长回文子串
- pycharm 无法引入自定义包
- Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
- Gpiof6, 7, 8 configuration
- Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
- 03 FastJson 解决循环引用
- LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
- Opencv Harris corner detection
- Pycharm cannot import custom package
猜你喜欢

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

Blue Bridge Cup for migrant workers majoring in electronic information engineering

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

For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer

Basic use and actual combat sharing of crash tool

CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)

03 fastjason solves circular references

LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)

Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)

使用密钥对的形式连接阿里云服务器
随机推荐
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
LeetCode - 715. Range 模块(TreeSet) *****
Swing transformer details-1
(2) New methods in the interface
Leetcode 300 最长上升子序列
Problems encountered when MySQL saves CSV files
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
Application of 51 single chip microcomputer timer
2.Elment Ui 日期选择器 格式化问题
Dictionary tree prefix tree trie
Synchronization control between tasks
Tensorflow2.0 save model
The underlying principle of vector
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
CV learning notes - feature extraction
My notes on intelligent charging pile development (II): overview of system hardware circuit design
(1) 什么是Lambda表达式
Screen display of charging pile design -- led driver ta6932
LeetCode - 933 最近的请求次数