当前位置:网站首页>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 - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
- Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
- 2021-10-27
- Simulate mouse click
- 2021-01-03
- Markdown latex full quantifier and existential quantifier (for all, existential)
- LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
- CV learning notes - camera model (Euclidean transformation and affine transformation)
- The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
- Not many people can finally bring their interests to college graduation
猜你喜欢
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
LeetCode - 706 设计哈希映射(设计) *
Octave instructions
Opencv notes 20 PCA
Interruption system of 51 single chip microcomputer
2.Elment Ui 日期选择器 格式化问题
LeetCode - 705 设计哈希集合(设计)
QT is a method of batch modifying the style of a certain type of control after naming the control
My notes on intelligent charging pile development (II): overview of system hardware circuit design
CV learning notes - image filter
随机推荐
Working mode of 80C51 Serial Port
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
El table X-axis direction (horizontal) scroll bar slides to the right by default
[keil5 debugging] warning:enumerated type mixed with other type
yocto 技术分享第四期:自定义增加软件包支持
Crash工具基本使用及实战分享
2. Elment UI date selector formatting problem
CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
Markdown latex full quantifier and existential quantifier (for all, existential)
Design of charging pile mqtt transplantation based on 4G EC20 module
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
(2) New methods in the interface
Gpiof6, 7, 8 configuration
Screen display of charging pile design -- led driver ta6932
Opencv feature extraction - hog
自動裝箱與拆箱了解嗎?原理是什麼?
Yocto technology sharing phase IV: customize and add software package support
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |