LeetCode:H-Index II

原创 2016年06月01日 19:20:35

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;
    }
}


版权声明:本文为博主原创文章,转载请注明出处。

<LeetCode OJ> 274 / 275 H-Index(I / II)

Total Accepted: 37719 Total Submissions: 125936 Difficulty: Medium Given an array of citations ...

【LeetCode从零单刷】H-index I & II

题目: Given an array of citations (each citation is a non-negative integer) of a researcher, writ...

Leetcode 274. H-Index & 275. H-Index II

274. H-Index Total Accepted: 49757 Total Submissions: 159122 Difficulty: Medium Given an a...

【LeetCode刷题记录】H-Index

题目Given an array of citations (each citation is a non-negative integer) of a researcher, write a fun...

【leetCode】H-Index

Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct...

leetcode274~H-Index

Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct...

leetcode_middle_87_274. H-Index

题意: 用插入排序来给一个单链表排序。 分析: 插入排序是遍历数组,然后,将这个数放在其左边(已经有序)的数组的有序位置。 但是链表不能倒序遍历,交换也很麻烦,我们用一个新数组来插...
  • pusude
  • pusude
  • 2017年03月09日 15:15
  • 142

LeetCode 274. H-Index(文献)

原题网址:https://leetcode.com/problems/h-index/ Given an array of citations (each citation is a non-ne...
  • jmspan
  • jmspan
  • 2016年04月13日 10:58
  • 348

leetcode题解-274. H-Index

题目:Given an array of citations (each citation is a non-negative integer) of a researcher, write a fu...

leetcode笔记:H-Index

给定一个数组,记载了某研究人员的文章引用次数(每篇文章的引用次数都是非负整数),编写函数计算该研究人员的h指数。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode:H-Index II
举报原因:
原因补充:

(最多只允许输入30个字)