File tree 6 files changed +182
-9
lines changed
solution/0200-0299/0275.H-Index II
6 files changed +182
-9
lines changed Original file line number Diff line number Diff line change 39
39
40
40
<!-- 这里可写通用的实现逻辑 -->
41
41
42
+ 二分查找。
43
+
42
44
<!-- tabs:start -->
43
45
44
46
### ** Python3**
45
47
46
48
<!-- 这里可写当前语言的特殊实现逻辑 -->
47
49
48
50
``` python
49
-
51
+ class Solution :
52
+ def hIndex (self , citations : List[int ]) -> int :
53
+ n = len (citations)
54
+ left, right = 0 , n
55
+ while left < right:
56
+ mid = (left + right) >> 1
57
+ if citations[mid] >= n - mid:
58
+ right = mid
59
+ else :
60
+ left = mid + 1
61
+ return n - left
50
62
```
51
63
52
64
### ** Java**
53
65
54
66
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
67
56
68
``` java
69
+ class Solution {
70
+ public int hIndex (int [] citations ) {
71
+ int n = citations. length;
72
+ int left = 0 , right = n;
73
+ while (left < right) {
74
+ int mid = (left + right) >>> 1 ;
75
+ if (citations[mid] >= n - mid) {
76
+ right = mid;
77
+ } else {
78
+ left = mid + 1 ;
79
+ }
80
+ }
81
+ return n - left;
82
+ }
83
+ }
84
+ ```
85
+
86
+ ### ** C++**
87
+
88
+ ``` cpp
89
+ class Solution {
90
+ public:
91
+ int hIndex(vector<int >& citations) {
92
+ int n = citations.size();
93
+ int left = 0, right = n;
94
+ while (left < right) {
95
+ int mid = left + right >> 1;
96
+ if (citations[ mid] >= n - mid) {
97
+ right = mid;
98
+ } else {
99
+ left = mid + 1;
100
+ }
101
+ }
102
+ return n - left;
103
+ }
104
+ };
105
+ ```
57
106
107
+ ### **Go**
108
+
109
+ ```go
110
+ func hIndex(citations []int) int {
111
+ n := len(citations)
112
+ left, right := 0, n
113
+ for left < right {
114
+ mid := (left + right) >> 1
115
+ if citations[mid] >= n-mid {
116
+ right = mid
117
+ } else {
118
+ left = mid + 1
119
+ }
120
+ }
121
+ return n - left
122
+ }
58
123
```
59
124
60
125
### ** ...**
Original file line number Diff line number Diff line change @@ -43,18 +43,83 @@ Since the researcher has 3 papers with at least 3 citations each and the remaini
43
43
44
44
## Solutions
45
45
46
+ Binary search.
47
+
46
48
<!-- tabs:start -->
47
49
48
50
### ** Python3**
49
51
50
52
``` python
51
-
53
+ class Solution :
54
+ def hIndex (self , citations : List[int ]) -> int :
55
+ n = len (citations)
56
+ left, right = 0 , n
57
+ while left < right:
58
+ mid = (left + right) >> 1
59
+ if citations[mid] >= n - mid:
60
+ right = mid
61
+ else :
62
+ left = mid + 1
63
+ return n - left
52
64
```
53
65
54
66
### ** Java**
55
67
56
68
``` java
69
+ class Solution {
70
+ public int hIndex (int [] citations ) {
71
+ int n = citations. length;
72
+ int left = 0 , right = n;
73
+ while (left < right) {
74
+ int mid = (left + right) >>> 1 ;
75
+ if (citations[mid] >= n - mid) {
76
+ right = mid;
77
+ } else {
78
+ left = mid + 1 ;
79
+ }
80
+ }
81
+ return n - left;
82
+ }
83
+ }
84
+ ```
85
+
86
+ ### ** C++**
87
+
88
+ ``` cpp
89
+ class Solution {
90
+ public:
91
+ int hIndex(vector<int >& citations) {
92
+ int n = citations.size();
93
+ int left = 0, right = n;
94
+ while (left < right) {
95
+ int mid = left + right >> 1;
96
+ if (citations[ mid] >= n - mid) {
97
+ right = mid;
98
+ } else {
99
+ left = mid + 1;
100
+ }
101
+ }
102
+ return n - left;
103
+ }
104
+ };
105
+ ```
57
106
107
+ ### **Go**
108
+
109
+ ```go
110
+ func hIndex(citations []int) int {
111
+ n := len(citations)
112
+ left, right := 0, n
113
+ for left < right {
114
+ mid := (left + right) >> 1
115
+ if citations[mid] >= n-mid {
116
+ right = mid
117
+ } else {
118
+ left = mid + 1
119
+ }
120
+ }
121
+ return n - left
122
+ }
58
123
```
59
124
60
125
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int hIndex (vector<int >& citations) {
4
+ int n = citations.size ();
5
+ int left = 0 , right = n;
6
+ while (left < right) {
7
+ int mid = left + right >> 1 ;
8
+ if (citations[mid] >= n - mid) {
9
+ right = mid;
10
+ } else {
11
+ left = mid + 1 ;
12
+ }
13
+ }
14
+ return n - left;
15
+ }
16
+ };
Original file line number Diff line number Diff line change
1
+ func hIndex (citations []int ) int {
2
+ n := len (citations )
3
+ left , right := 0 , n
4
+ for left < right {
5
+ mid := (left + right ) >> 1
6
+ if citations [mid ] >= n - mid {
7
+ right = mid
8
+ } else {
9
+ left = mid + 1
10
+ }
11
+ }
12
+ return n - left
13
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int hIndex (int [] citations ) {
3
3
int n = citations .length ;
4
- int l = 0 , r = n ;
5
- while (l < r ) {
6
- int mid = l + r + 1 >>> 1 ;
7
- if (citations [n - mid ] >= mid ) l = mid ;
8
- else r = mid - 1 ;
4
+ int left = 0 , right = n ;
5
+ while (left < right ) {
6
+ int mid = (left + right ) >>> 1 ;
7
+ if (citations [mid ] >= n - mid ) {
8
+ right = mid ;
9
+ } else {
10
+ left = mid + 1 ;
11
+ }
9
12
}
10
- return r ;
13
+ return n - left ;
11
14
}
12
- }
15
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def hIndex (self , citations : List [int ]) -> int :
3
+ n = len (citations )
4
+ left , right = 0 , n
5
+ while left < right :
6
+ mid = (left + right ) >> 1
7
+ if citations [mid ] >= n - mid :
8
+ right = mid
9
+ else :
10
+ left = mid + 1
11
+ return n - left
You can’t perform that action at this time.
0 commit comments