leetcode_c++:哈希:H-Index(274)
2016-07-10 14:41
519 查看
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.
以上。
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.
O(nlgn)
对于给定的而一个数组,可以先进行排序。逐渐找出引用最多的文章加入到集合中,每次加入一个元素集合就增大一个,也就是h增加一。当加入一个新的元素是,这个元素的引用次数小于h+1时就返回此时的h,否则,将h加1。以上。
class Solution { public: int hIndex(vector<int>& citations) { sort(citations.begin(), citations.end()); int h = 0; auto iter = citations.rbegin(); while(iter != citations.rend()) { h++; if(*iter < h)return h - 1; iter++; } return h; } };
相关文章推荐
- leetcode_c++:哈希: Valid Anagram(242)
- "const" & "#define"
- 读Advanced C and C++ Compiling
- leetcode_c++:哈希: Isomorphic Strings(205)
- Kruskal最小生成树
- 杂谈c++与java
- 贪心算法-Huffman编码
- leetcode_c++:哈希:Count Primes (204)
- 洛谷1341 无序字母对 解题报告
- C++位运算知识点
- C语言实验——保留字母
- C语言 百炼成钢25
- C语言atoi()函数:将字符串转换成int(整数)
- C语言link()函数:建立文件连接
- C语言lseek()函数:移动文件的读写位置
- 求两圆相交部分面积 c++
- c++中的.hpp文件
- neuq oj C++ 二维数组1064: 谭浩强C语言(第三版)习题8.4
- 背包问题
- C++11中的进行线程同步的方法