当前位置:网站首页>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;
}
边栏推荐
- Pymssql controls SQL for Chinese queries
- Simulate mouse click
- Interruption system of 51 single chip microcomputer
- 自動裝箱與拆箱了解嗎?原理是什麼?
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- STM32 general timer 1s delay to realize LED flashing
- 01仿B站项目业务架构
- MySQL root user needs sudo login
- My notes on intelligent charging pile development (II): overview of system hardware circuit design
- Google browser plug-in recommendation
猜你喜欢

51 MCU tmod and timer configuration

CV learning notes - clustering

Basic knowledge of communication interface

Opencv Harris corner detection
![[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)](/img/19/5dc152b3fadeb56de50768561ad659.jpg)
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)

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

CV learning notes - scale invariant feature transformation (SIFT)

LeetCode - 706 设计哈希映射(设计) *

It is difficult to quantify the extent to which a single-chip computer can find a job

LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
随机推荐
Problems encountered when MySQL saves CSV files
LeetCode - 919. Full binary tree inserter (array)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
STM32 interrupt switch
Liquid crystal display
Opencv image rotation
Dynamic layout management
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Emballage automatique et déballage compris? Quel est le principe?
2020-08-23
Opencv feature extraction sift
Swing transformer details-2
Gpiof6, 7, 8 configuration
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
LeetCode - 5 最长回文子串
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
LeetCode - 705 设计哈希集合(设计)
Windows下MySQL的安装和删除
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
2020-08-23