题目
解题思路
- 数组是升序排列的,所以可以从后向前遍历;
- 遍历过的元素肯定大于等于当前元素的值,遍历过的元素数量为n - i;
- 只要当前元素的值大于等于遍历过的元素数量,既可作为结果之一;
- 选择最后结果最大值输出。
代码展示
class Solution {
public int hIndex(int[] citations) {
int ans = 0;
int n = citations.length;
for (int i = n - 1; i >= 0; i--){
if(citations[i] >= n - i){
ans = n - i;
}
}
return ans;
}
}