-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHIndexII.java
More file actions
62 lines (57 loc) · 1.59 KB
/
HIndexII.java
File metadata and controls
62 lines (57 loc) · 1.59 KB
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
package leetcode;
/**
* HIndexII
* https://leetcode-cn.com/problems/h-index-ii/
* 275. H 指数 II
* https://leetcode-cn.com/problems/h-index-ii/solution/jian-dan-dian-ti-jie-jiu-jian-dan-dian-b-icfn/
*
* @author tobin
* @since 2021-07-12
*/
public class HIndexII {
public static void main(String[] args) {
HIndexII sol = new HIndexII();
System.out.println(sol.hIndex(new int[]{0, 1, 3, 5, 6}));
System.out.println(sol.hIndex(new int[]{100}));
System.out.println(sol.hIndex(new int[]{0}));
System.out.println(sol.hIndex(new int[]{11, 15}));
}
public int hIndex(int[] citations) {
for (int h = citations.length; h > 0; h--) {
int idx = citations.length - h;
if (citations[idx] >= h) {
return h;
}
}
return 0;
}
// public int hIndex(int[] citations) {
// int start = 0;
// int end = citations.length - 1;
// while (start <= end) {
// if (end - start < 2) {
// break;
// }
// int mid = (start + end) / 2;
//
// int h = citations[mid];
// int h1 = citations.length - mid;
// if (h > h1) {
// end = mid;
// } else {
// start = mid;
// }
// }
//
// int h_e = citations[end];
// if (h_e <= (citations.length - end)) {
// return h_e;
// }
// int h_s = citations[start];
//
// if (h_s <= (citations.length - start)) {
// return h_s;
// }
// return 1;
// }
}