关闭

LeetCode:H-Index II

标签: LeetCodeBinary Search
2369人阅读 评论(0) 收藏 举报
分类:

H-Index II




Total Accepted: 24331 Total Submissions: 74459 Difficulty: Medium

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

Hint:

  1. Expected runtime complexity is in O(log n) and the input is sorted.

Subscribe to see which companies asked this question

Hide Tags
 Binary Search
Hide Similar Problems
 (M) H-Index
















思路:

承接上题【】,与上题不同之处在于已经是排序的,因此通过二分找到临界点即可。


java code:

public class Solution {
    public int hIndex(int[] citations) {
        
        if(citations == null || citations.length == 0) return 0;
        
        int len = citations.length;
        int lo = 0, hi = len-1;
        
        while(lo <= hi) {
            int mid = lo + (hi-lo)/2;
            
            if(citations[mid] == len - mid)
                return len-mid;
            else if(citations[mid] >= len - mid)
                hi = mid - 1;
            else 
                lo = mid + 1;
        }
        
        return len-lo;
    }
}


0
0
查看评论

leetcode 275: H-Index II

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? [思路] 二分查找, [CODE] p...
  • xudli
  • xudli
  • 2015-09-05 17:09
  • 2831

leetcode 274: H-Index

H-Index Total Accepted: 2684 Total Submissions: 11955 Given an array of citations (each citation is a non-negative integer) of a res...
  • xudli
  • xudli
  • 2015-09-05 16:30
  • 3776

LeetCode刷题 (Python) | 275. H-Index II

LeetCode刷题 (Python) | 275. H-Index II
  • Titan0427
  • Titan0427
  • 2016-02-10 17:48
  • 811

leetcode笔记:H-Index

给定一个数组,记载了某研究人员的文章引用次数(每篇文章的引用次数都是非负整数),编写函数计算该研究人员的h指数。
  • liyuefeilong
  • liyuefeilong
  • 2016-02-01 15:31
  • 2062

LeetCode H-index and H-index II

题目: Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index....
  • sun_wangdong
  • sun_wangdong
  • 2015-12-18 14:38
  • 238

leetcode 275. H-Index II

What if the citations array is sorted in ascending order? Could you optimize your algorithm? int hIndex(int* citations, int cit...
  • u014568921
  • u014568921
  • 2016-03-31 20:26
  • 267

LeetCode275——H-Index II

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? 实现: clas...
  • booirror
  • booirror
  • 2015-09-17 10:14
  • 1007

LeetCode H-Index II 数组+二分

思路:根据上一题优化而来,既然是查找一个临界状态,又要求时间复杂度O(logN),用二分查找。临界条件:找到一个 citation[i] == len - i 的点; 如果 citation[i] > len - i ,说明还可以往左试探; 如果 citation[i] < len ...
  • yeruby
  • yeruby
  • 2015-11-16 20:31
  • 458

[leetcode] 274.H-Index

题目: Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index.Acco...
  • u014673347
  • u014673347
  • 2015-09-05 16:58
  • 1101

[leetcode] 275.H-Index II

题目: Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? 题意: 这道题目是接274题而来,不过这次的引用次数是...
  • u014673347
  • u014673347
  • 2015-09-05 17:00
  • 535
    个人资料
    • 访问:390376次
    • 积分:6789
    • 等级:
    • 排名:第4134名
    • 原创:321篇
    • 转载:1篇
    • 译文:0篇
    • 评论:34条
    博客专栏
    最新评论