goden1710

package
v0.0.0-...-b071cee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2023 License: GPL-3.0 Imports: 0 Imported by: 0

README

面试题 17.10.主要元素

1. 题目描述

数组中占比超过一半的元素称之为主要元素。给你一个 整数 数组,找出其中的主要元素。若没有,返回 -1 。请设计时间复杂度为 O(N) 、空间复杂度为 O(1) 的解决方案。   示例 1:


输入:[1,2,5,9,5,9,5,5,5]
输出:5

示例 2:


输入:[3,2]
输出:-1

示例 3:


输入:[2,2,1,1,1,2,2]
输出:2

标签 数组 计数

2. 解题

投票法

每个元素有一票,主要元素必以多数票当选 curNum: 当前备选数字 tickets: 当前备选数字比其他数字多几个

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL