[Leetcode Week4]H-Index
H-Index题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/h-index/description/
Description
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.
Solution
typedef struct range {
int start, end;
} range;
range new_range(int s, int e) {
range r;
r.start = s;
r.end = e;
return r;
}
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void quickSort(int arr[], int size) {
if (size <= 1)
return;
range rangeStack[size], tempRange;
int pos = 0, mid, left, right;
rangeStack[pos++] = new_range(0, size - 1);
while (pos) {
tempRange = rangeStack[--pos];
if (tempRange.start >= tempRange.end)
continue;
left = tempRange.start;
right = tempRange.end - 1;
mid = arr[tempRange.end];
while (left < right) {
while (arr[left] < mid && left < right)
left++;
while (arr[right] >= mid && left < right)
right--;
swap(&arr[left], &arr[right]);
}
if (arr[left] >= arr[tempRange.end])
swap(&arr[left], &arr[tempRange.end]);
else
left++;
rangeStack[pos++] = new_range(tempRange.start, left - 1);
rangeStack[pos++] = new_range(left + 1, tempRange.end);
}
}
int hIndex(int* citations, int citationsSize) {
if (citationsSize <= 0)
return 0;
int i;
quickSort(citations, citationsSize);
for (i = citationsSize - 1; i >= 0; i--) {
if (citations[i] >= citationsSize - i) {
if (i == 0) {
return citationsSize;
} else if (citations[i - 1] <= citationsSize - i) {
return citationsSize - i;
}
}
}
return 0;
}
解题描述
这道题我考虑的算法是先将给定的数组进行排序,之后再从后往前检测数组,查看是否存在所求的H指数。我的解答中自己写了用迭代实现的快排
[Leetcode Week4]H-Index的更多相关文章
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- LeetCode 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...
- [LeetCode] Find Pivot Index 寻找中枢点
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- LeetCode 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...
- 【LeetCode】1065. Index Pairs of a String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- Leetcode: Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- [Leetcode Week4]Merge Two Sorted Lists
Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...
- [Leetcode Week4]Course Schedule II
Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...
- Leetcode 274.H指数
H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...
随机推荐
- 杀死 tomcat 进程的脚本
新建一个.sh 文件 把下面的内容复制进去.然后 把这个文件放到tomcat 的bin目录下在关闭tomcat 执行这个脚本. 可以解决 在关闭tomcat的时候 总是遗留一些tomcat进程没有结束 ...
- Python登录小程序
------------------------------------------------- 主要实现功能 1.用户输入用户名,在用户名文件中查找对应的用户,若无对应用户名则打印输入错误 2.用 ...
- 无法注册DLL/OCX:regsvr32 failed with exit code 0x5
最近在服务器上装一个等值线控件,确报错: RegSvr32 failed with exit code 0x5 尝试解决方法1: 在cmd下 运行for %1 in (%windir%\syste ...
- oracle ocp 052考试学习
1.数据字典存储在SYSTEM表空间中. 2.SYSAUX可以offline: SQL>alter tablespace sysaux offline; 3.SYSTEM和SYSAUX都是永久表 ...
- js 给某个div增加class 样式(三种方式)
第一种: el.setAttribute('class','abc'); <!DOCTYPE HTML> <HTML> <HEAD> <meta c ...
- delphi如何模块内部获得自身路径ExtractFilePath和paramstr(0)
如何模块内部获得自身路径?Exe程序:DLL程序: ExtractFilePath(Application.ExeName) GetCurrentDir TIniFile.Create(GetCurr ...
- 简单配置 Tomcat 的 Server.xml(war包发布)
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" ...
- 【bzoj2659】[Beijing wc2012]算不出的算式 数论
题目描述 求,其中p和q是奇质数. 输入 只有一行,两个奇质数,分别表示p,q. 输出 一个数,表示算式结果. 样例输入 5 样例输出 6 题解 数论 神TM数学结论题... 当$p\neq q$时, ...
- 【ZJ选讲·BZOJ 5071】
小A的数字 有一串数字A1 ,A2,--,An,每次可以进行如下骚操作: 选择一个数字i,将(Ai-1,Ai,Ai+1)变为(Ai-1+Ai,-Ai,Ai+1+Ai), (特别地,若i=N,则( ...
- 安卓下拉刷新空间SwipeRefreshLayout的基本使用
1.先写布局文件 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh" androi ...