Skip to content

Commit 7a03cf9

Browse files
committed
feat: add solutions to lc problem: No.0275. H-Index II
1 parent 9f8ed6d commit 7a03cf9

File tree

6 files changed

+182
-9
lines changed

6 files changed

+182
-9
lines changed

solution/0200-0299/0275.H-Index II/README.md

+66-1
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,87 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
二分查找。
43+
4244
<!-- tabs:start -->
4345

4446
### **Python3**
4547

4648
<!-- 这里可写当前语言的特殊实现逻辑 -->
4749

4850
```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
5062
```
5163

5264
### **Java**
5365

5466
<!-- 这里可写当前语言的特殊实现逻辑 -->
5567

5668
```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+
```
57106
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+
}
58123
```
59124

60125
### **...**

solution/0200-0299/0275.H-Index II/README_EN.md

+66-1
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,83 @@ Since the researcher has 3 papers with at least 3 citations each and the remaini
4343

4444
## Solutions
4545

46+
Binary search.
47+
4648
<!-- tabs:start -->
4749

4850
### **Python3**
4951

5052
```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
5264
```
5365

5466
### **Java**
5567

5668
```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+
```
57106
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+
}
58123
```
59124

60125
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
class Solution {
22
public int hIndex(int[] citations) {
33
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+
}
912
}
10-
return r;
13+
return n - left;
1114
}
12-
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

0 commit comments

Comments
 (0)