[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 篇论文中至多有 ...
随机推荐
- Qt Charts_Audio实践
这里完全是照搬帮助文档中的代码生成的程序 上预览图 工程文件代码 #------------------------------------------------- # # Project crea ...
- js中DOM 节点的一些操作方法
什么是DOM DOM:文档对象模型.DOM 为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构.目的其实就是为了能让js操作html元素而制定的一个规范. DOM就是由节点组成的. 解析过程 ...
- 为Zabbix配置Nova服务、Keystone和Placement进程CPU和内存usage监控
目前已经完成了RabbitMQ和MySQL的监控项配置,还差对nova-api.nova-conductor.nova-scheduler和keystone进程CPU和内存 usage的监控,类似的轮 ...
- Leetcode 678.有效的括号字符串
有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何右括号 ) 必须 ...
- Go基础篇【第1篇】: 内置库模块 OS
os包提供了操作系统函数的不依赖平台的接口.设计为Unix风格的,虽然错误处理是go风格的:失败的调用会返回错误值而非错误码.通常错误值里包含更多信息.os包的接口规定为在所有操作系统中都是一致的.非 ...
- 前台如何处理后台返回的json数据
后台返回的json数据格式: { "state": true, "data": { "id": 0, "name": & ...
- Python如何进行中文注释
最近,由于实习工作的需要,开始接触Python,但是第一个大的脚本写下来之后,连中文注释都没办法加,很郁闷,遂在网上找解决办法,在Python 官网上看到这个页面:http://www.python. ...
- 【积累】根据CheckBox的不选中 ,用JQuery 清除 RidaoButtonList 的选中项
如题,项目要求无刷新更新数据. 1)Web页面布局 Html以及效果图
- webstorm-前端javascript开发神器中文教程和技巧分享(转)
webstorm是一款前端javascript开发编辑的神器,此文介绍webstorm的中文教程和技巧分享. webstorm8.0.3中文汉化版下载: 百度网盘下载:http://pan.baidu ...
- 【Autofac】- 创建的类的生命周期
1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component so tha ...