leetcode 275. H-Index II 高引用次数计算2

谁践踏了优雅 2022-06-08 13:26 250阅读 0赞

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

和上一道题一样leetcode 274. H-Index,不过不需要排序,其余的都一样。

代码如下:

  1. /*
  2. * https://baike.baidu.com/item/h-index/3991452?fr=aladdin
  3. * 参见H-Index的百科即可
  4. * 这里是升序,那么就不要排序了
  5. * */
  6. class Solution
  7. {
  8. public int hIndex(int[] citations)
  9. {
  10. if(citations==null || citations.length<=0)
  11. return 0;
  12. //Arrays.sort(citations);
  13. int maxH=0;
  14. for(int i=citations.length-1;i>=0;i--)
  15. {
  16. int count=citations.length-i;
  17. if(citations[i]>=count && count>maxH)
  18. maxH=count;
  19. else
  20. break;
  21. }
  22. return maxH;
  23. }
  24. }

下面是C++的做法,直接按照H-Index的做法即可,代码如下:

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <set>
  5. #include <queue>
  6. #include <stack>
  7. #include <string>
  8. #include <algorithm>
  9. using namespace std;
  10. class Solution
  11. {
  12. public:
  13. int hIndex(vector<int>& c)
  14. {
  15. //sort(c.begin(),c.end());
  16. int maxH = 0;
  17. for (int i = c.size() - 1; i >= 0; i--)
  18. {
  19. int count = c.size() - i;
  20. if (c[i] >= count && count > maxH)
  21. maxH = count;
  22. }
  23. return maxH;
  24. }
  25. };

发表评论

表情:
评论列表 (有 0 条评论,250人围观)

还没有评论,来说两句吧...

相关阅读

    相关 275. H 指数 II

    给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照 升序排列 。编写一个方法,计算出研究者的 h 指数。 h 指数的定义: “h 代表“高引用次数”(h