题目

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.

According to the definition of h-index on Wikipedia: “A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each.”

For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.

分析

题目描述的要求如下:

给定一个整数序列 citations=[3,0,6,1,5],代表研究人员共有5篇论文,每个元素代表该论文的引用数量。从序列元素可以看出,该研究人员有至少3篇论文引用数量为>=3的,其余2篇论文引用数量不足3个引用,所以返回他的 h−index=3;

也就是说,我们找返回一个整数h,使得数组中至少h个元素值大小>=h,其n−h个元素值<h。

解决方法:

首先对序列排序,然后从大到小遍历数组,h值为从1到n,若元素满足num[i]>h,继续遍历,否则跳出循环,返回h即可。

AC代码

  1. class Solution {
  2. public:
  3. int hIndex(vector<int>& citations) {
  4. if (citations.empty())
  5. return 0;
  6. //对所给序列排序
  7. sort(citations.begin(), citations.end());
  8. int len = citations.size(),maxH = 0;
  9. for (int i = len - 1; i >= 0; --i)
  10. {
  11. int h = len - i;
  12. if (citations[i] >= h && h > maxH)
  13. {
  14. maxH = h;
  15. }
  16. else{
  17. break;
  18. }
  19. }//for
  20. return maxH;
  21. }
  22. };

GitHub测试程序源码

LeetCode(274)H-Index的更多相关文章

  1. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  2. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

  3. Leetcode(3)无重复字符的最长子串

    Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...

  4. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  5. Leetcode(6)Z字形变换

    Leetcode(6)Z字形变换 [题目表述]: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...

  6. Leetcode(8)字符串转换整数

    Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...

  7. iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):2 H264数据写入文件

    本文档为iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):1 概述续篇,主要描述: CMSampleBufferRef读取实际数据 序列参数集(Sequence Para ...

  8. iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):1 概述

    本文档尝试用Video Toolbox进行H.265(HEVC)硬件编码,视频源为iPhone后置摄像头.去年做完硬解H.264,没做编码,技能上感觉有些缺失.正好刚才发现CMFormatDescri ...

  9. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

随机推荐

  1. 低版本Firefox支持innerText属性兼容方法

    FireFox支持innerText属性了,很遗憾是44.0.2版本以下还需要兼容处理. 方法一: innerHTML是符合W3C标准的属性,而innerText只适用于IE浏览器,因此,尽可能地去使 ...

  2. [转]Todd.log - a place to keep my thoughts on programming 分布式架构中的幂等性

    Todd.log - a place to keep my thoughts on programming 理解HTTP幂等性 基于HTTP协议的Web API是时下最为流行的一种分布式服务提供方式. ...

  3. JAVA基础之File类

    个人理解: File是个文件类,可以用其增加.删除.查找某种类型的文件或者文件夹,同时根据其成员变量的特点可以综合利用,避免出现跨系统的时候出现错误,并且查找时最好输入绝对路径,以免出现不存在的文件. ...

  4. nmap扫描开放端口

    nmap 192.168.1.1  -p1-65535 指定端口范围使用-p参数,如果不指定要扫描的端口,Nmap默认扫描从1到1024再加上nmap-services列出的端口 nmap-servi ...

  5. Linux生产服务器常规分区方案

    常规分区方案 / 剩余硬盘大小 swap 100M /boot 100M DB及存储:有大量重要的数据 /data/ 剩余硬盘大小 / 50-200GB swap 1.5倍 /boot 100MB 门 ...

  6. C++ 中函数后面跟const是什么意思

    问题:c++:void display( ) const 中的const是什么意思?简答:意思是除了表明了mutable的成员变量以外该类的其他的成员变量在这个函数内一律不能修改. 详细:加const ...

  7. django之session配置

    session应用示例 from django.shortcuts import render from django.shortcuts import HttpResponse from djang ...

  8. javascript 和Jquery 互转

    jQuery对象转成DOM对象: 两种转换方式将一个jQuery对象转换成DOM对象:[index]和.get(index); (1)jQuery对象是一个数据对象,可以通过[index]的方法,来得 ...

  9. 51nod 1572 宝岛地图

    题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 勇敢的水手们到达了一个小岛,在这个小岛上,曾经有海盗在这里埋下了一些宝藏.然而,我 ...

  10. UWP开发:存储容器设置&复合设置数据

    有时候为了将应用设置进行分类,需要创建新的容器进行存储应用设置的信息. 1,容器的创建:在一个根容器里嵌套一个新容器 1)首先获取根容器. 2)调用ApplicationDataContainer.C ...