week28
274 - H指数
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end(), greater<int>());
// 6 5 3 1 0 满足f[i] >= i
for(int i = citations.size() - 1; i >= 0; i--)
if(citations[i] >= i + 1) return i + 1;
return 0;
}
};275 - H指数II
class Solution {
public:
int hIndex(vector<int>& citations) {
// 这题已经保证有序了 但是是从小到大的有序
int n = citations.size();
if(n == 0) return 0;
reverse(citations.begin(), citations.end());
int l = 0, r = n - 1;
while(l < r)
{
int mid = l + r + 1 >> 1;
if(citations[mid] >= mid + 1) l = mid;
else r = mid - 1;
}
if(citations[l] >= l + 1) return l + 1;
else return l;
}
};278 - 第一个错误的版本
279 - 完全平方数
Last updated