IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]LeetCode -- H-Index II

    csharp25发表于 2015-11-16 18:17:25
    love 0
    题目描述:
    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?


    与H-Index的区别就是,这个数组是倒序排序好的。


    思路:
    省去了排序的步骤,直接从数组尾部向前遍历就可以了。
    其余思路与H-index相同。




    实现代码:


    public class Solution {
        public int HIndex(int[] citations) 
        {
            if(citations.Length == 0){
        		return 0;
        	}
        	
        	var sum = 0;
        	var j = 0;
        	for(var i = citations.Length - 1;i >= 0; i--){
        		if(j < citations[i]){
        			sum ++;
        		}
        		j++;
        	}
        	
        	return sum;
        }
    }




沪ICP备19023445号-2号
友情链接