[LeetCode]275. H-Index II

来源:互联网 发布:sql for in 循环语句 编辑:程序博客网 时间:2024/06/08 07:05

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?


思路:这个就是给你从小到大排序过的数组了,可以直接用上一个算法解决了。。。

让h先等于n(能取到的最大数),然后用nums[n-h]与h相比较,因为大于nums[n-h]的数量正好为h个,如果nums[n-h]>=h,则说明存在有h个大于等于h的数,如果不是则把h减一,循环继续找

AC之后看了下统计结果。。。发现很差很差。。。还有更好的算法

public class Solution {    public int hIndex(int[] citations) {        int n=citations.length;        int h=n;        while(h!=0){            if(citations[n-h]>=h){                return h;            }else{                h--;            }        }        return 0;    }}


0 0
原创粉丝点击