Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
JsonChao
Awesome-Algorithm-Study
Repository navigation
Code
Issues
1
(1)
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Awesome-Algorithm-Study
/
data_struct_study
/
src
/
array_problem
/
Solution_2.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
47 lines (40 loc) · 2.18 KB
master
Breadcrumbs
Awesome-Algorithm-Study
/
data_struct_study
/
src
/
array_problem
/
Solution_2.java
Copy path
Top
File metadata and controls
Code
Blame
47 lines (40 loc) · 2.18 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package
array_problem
;
/**
* 多数投票问题,可以利用 Boyer-Moore 投票算法
* 来解决这个问题,使得时间复杂度为 O(n)。
*
* 使用 cnt 来统计一个元素出现的次数,当遍历到的元素和统计元素相等时,
* 令 cnt++,否则令 cnt--。如果前面查找了 i 个元素,且 cnt == 0,
* 说明前 i 个元素没有 majority,或者有 majority,但是出现的次数
* 少于 i / 2 ,因为如果多于 i / 2 的话 cnt 就一定不会为 0 。
* 此时剩下的 n - i 个元素中,majority 的数目依然多于 (n - i) / 2,
* 因此继续查找就能找出 majority。
*/
public
class
Solution_2
{
// 1、哈希表:用hashMap统计所有元素出现的次数,时间复杂度O(n), 空间复杂度O(n)
// 2、排序:如果将数组 nums 中的所有元素按照单调递增或单调递减的顺序排序,
// 那么下标为n/2的元素一定是众数。时间复杂度O(nlog(n)),空间复杂度O(logn)(自己实现堆排序O(1))
// 3、Boyer Moore投票算法:时间复杂度O(n), 空间复杂度O(1)
public
int
majorityElement
(
int
[]
nums
) {
// 1、初始化出现次数和候选众数
int
count
=
0
;
Integer
candidate
=
null
;
// 2、遍历数组
for
(
int
num
:
nums
) {
// 3、如果出现次数为0,则更新当前的候选众数
if
(
count
==
0
) {
candidate
=
num
;
}
// 4、然后更新当前候选众数的出现次数:当前元素与候选众数相等,则+1,否则-1
count
+= (
num
==
candidate
) ?
1
: -
1
;
}
// 5、最后的候选众数就是真正的众数
// 为什么?如果候选众数不是真正的众数,则真正众数会和其它非候选人一起反对,此时候选众数就会下台(count==0),
// 如果候选众数是真正的众数,则真正的众数就会支持,其它非获选人会一起反对,此时候选众数最后一定会当选。
return
candidate
;
}
public
static
void
main
(
String
[]
args
) {
int
[]
nums
=
new
int
[]{
2
,
2
,
1
,
1
,
1
,
2
,
2
};
System
.
out
.
println
(
new
Solution_2
().
majorityElement
(
nums
));
}
}
You can’t perform that action at this time.