思路很简单,排序,然后判断后面的位数(后面每个数大于等于当前数)是否多于当前数,从后往前遍历剪枝
cppclass Solution {
public:
int hIndex(vector<int>& citations) {
std::sort(citations.begin(), citations.end()); // 将数组排序
int h = 0;
int n = citations.size();
for (int i = n - 1; i >= 0; i--) {
// 找到满足条件的最大 h 值
if (citations[i] >= n - i) {
h = n - i;
} else {
break;
}
}
return h;
}
};
本文作者:yowayimono
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!