###### tags: `LeetCode` `Medium` # LeetCode #274 [H-Index](https://leetcode.com/problems/h-index) ### (Medium) 給你一個整數數組 citations ,其中 citations[i] 表示研究者的第 i 篇論文被引用的次數。計算並返回該研究者的 h 指數。 h 指數的定義:h 代表“高引用次數”(high citations),一名科研人員的 h 指數是指他(她)的 (n 篇論文中)總共有 h 篇論文分別被引用了至少 h 次。且其餘的 n — h 篇論文每篇被引用次數 不超過 h 次。 例如:某人的 h 指數是 20,這表示他已發表的論文中,每篇被引用了至少 20 次的論文總共有 20 篇。 提示:如果 h 有多種可能的值,h 指數 是其中最大的那個。 --- 建立大小為n+1的vector “hash”, 遍歷citations, 大於等於n的數存入hash[n], 其餘數存入hash[citations[i]]。 完成後可得一vector存有被引用次數的個數。 接著從i=n開始迴圈到i=0, 另一變數count儲存hash[i]的值, 當count ≥i時, 代表被引用數≥i的論文篇數多於或等於i篇, 此時i即為答案。 --- ``` class Solution { public: int hIndex(vector<int>& citations) { if(citations.size()){ int n = citations.size(); vector<int> hash (n+1,0); for(int i=0;i<n;i++){ if(citations[i]>=n)hash[n]++; else hash[citations[i]]++; } int ans=0; for(int i=n;i>=0;i--){ cout<<hash[i]; ans+=hash[i]; if(ans>=i)return i; } return 1; } return 0; } }; ```