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指数。我的解答中自己写了用迭代实现的快排

最新文章

  1. Java的动态绑定机制
  2. Jquery禁止/恢复按钮与文本框代码
  3. 更新日志 - fir.im 回归,上线 Android Studio 插件
  4. 【jQuery基础学习】03 jQuery中的事件与动画
  5. Java中泛型在集合框架中的应用
  6. Windows程序设计-窗口和消息
  7. JDBC链接MySQL和Oracle
  8. Android进阶笔记17:3种JSON解析工具(org.json、fastjson、gson)
  9. 怎样把SEL放进NSArray里
  10. Eclipse 乱码解决方案(UTF8 -- GBK)
  11. Oracle SecureFiles 说明(转)
  12. php des 加密类
  13. C语言位运算符:与、或、异或、取反、左移和右移
  14. 在vue中优雅地实现简单页面逆传值
  15. Windows上IOCP Socket事件模型管理
  16. Delphi全局热键的注册
  17. C# 基础之string[,] 和string[][]
  18. ios中设置input为readonly后,解决弹起软键盘的问题
  19. Debug 路漫漫-06
  20. git源码阅读

热门文章

  1. Linux 文件属性及修改权限
  2. 排查实时tail功能cpu占用过高问题
  3. 搭建Hadoop环境(一)
  4. el-input为数字时验证问题
  5. [C/C++] C++常见面试题
  6. jcaptcha配置验证码
  7. 【题解】NOI2009二叉查找树 + NOIP2003加分二叉树
  8. [bzoj] 2038 小Z的袜子(hose) || 莫队
  9. BZOJ1513 [POI2006]Tet-Tetris 3D 【二维线段树】
  10. [学习笔记]可持久化数据结构——数组、并查集、平衡树、Trie树