-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode274.java
More file actions
44 lines (40 loc) · 1.31 KB
/
LeetCode274.java
File metadata and controls
44 lines (40 loc) · 1.31 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
public class LeetCode274 {
public static void main(String[] args) {
// 输入:citations = [3,0,6,1,5]
// 输出:3
System.out.println(new Solution274().hIndex(new int[] { 3, 0, 6, 1, 5 }));
// 输入:citations = [1,3,1]
// 输出:1
System.out.println(new Solution274().hIndex(new int[] { 1, 3, 1 }));
// 输入:citations = [4,4,0,0]
// 输出:2
System.out.println(new Solution274().hIndex(new int[] { 4, 4, 0, 0 }));
}
}
class Solution274 {
public int hIndex(int[] citations) {
int h = 0;
for (int i = 0; i < citations.length; i++) {
int currentMaxIndex = i;
int currentMaxValue = citations[i];
for (int j = i + 1; j < citations.length; j++) {
if (citations[j] > currentMaxValue) {
currentMaxIndex = j;
currentMaxValue = citations[j];
}
}
swap(citations, i, currentMaxIndex);
if (currentMaxValue < i + 1) {
return h;
} else {
h++;
}
}
return h;
}
public void swap(int[] citations, int i, int j) {
int temp = citations[i];
citations[i] = citations[j];
citations[j] = temp;
}
}