FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode/src/main/java/L0275_HIndexII.java at master · LjyYano/LeetCode · GitHub
LjyYano
/
LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
125
Star
342
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0275_HIndexII.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (56 loc) · 2.35 KB
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0275_HIndexII.java
Copy path
File metadata and controls
64 lines (56 loc) · 2.35 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* https://leetcode.cn/problems/h-index-ii/
*
* 给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数,citations 已经按照 升序排列 。计算并返回该研究者的 h 指数。
*
* h 指数的定义:h 代表"高引用次数"(high citations),一名科研人员的 h 指数是指他(她)的 (n 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。
*
* 请你设计并实现对数时间复杂度的算法解决此问题。
*
* 示例 1:
* 输入:citations = [0,1,3,5,6]
* 输出:3
* 解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 0, 1, 3, 5, 6 次。
* 由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。
*
* 示例 2:
* 输入:citations = [1,2,100]
* 输出:2
*
* 提示:
* n == citations.length
* 1 <= n <= 10⁵
* 0 <= citations[i] <= 1000
* citations 按 升序排列
*/
public
class
L0275_HIndexII
{
// 使用二分查找
// 因为数组已经排序,所以可以用二分查找优化
public
int
hIndex
(
int
[]
citations
) {
int
n
=
citations
.
length
;
int
left
=
0
;
int
right
=
n
-
1
;
// 二分查找第一个满足条件的位置
// citations[i] >= n-i 表示从位置 i 到末尾有 n-i 篇论文,引用次数都大于等于 citations[i]
while
(
left
<=
right
) {
int
mid
=
left
+ (
right
-
left
) /
2
;
if
(
citations
[
mid
] >=
n
-
mid
) {
right
=
mid
-
1
;
}
else
{
left
=
mid
+
1
;
}
}
return
n
-
left
;
}
public
static
void
main
(
String
[]
args
) {
L0275_HIndexII
solution
=
new
L0275_HIndexII
();
// 测试用例 1
System
.
out
.
println
(
solution
.
hIndex
(
new
int
[]{
0
,
1
,
3
,
5
,
6
}));
// 应该输出 3
// 测试用例 2
System
.
out
.
println
(
solution
.
hIndex
(
new
int
[]{
1
,
2
,
100
}));
// 应该输出 2
// 测试用例 3:全部为 0
System
.
out
.
println
(
solution
.
hIndex
(
new
int
[]{
0
,
0
,
0
}));
// 应该输出 0
// 测试用例 4:全部为较大数
System
.
out
.
println
(
solution
.
hIndex
(
new
int
[]{
5
,
5
,
5
,
5
,
5
}));
// 应该输出 5
}
}
Back
|
FazBrowse Home
|
New Git URL