Sort (2) -- H-Index I, II

H-Index算法解析
本文介绍了H-Index的定义及两种高效算法实现方法。一种是通过计数排序思想,使用数组记录不同引用值的文章数量,并从高引用数开始计算满足H-Index条件的最大值;另一种则是基于快速排序的思想,通过迭代寻找枢纽点来定位H-Index。

H-Index

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.

解法1:使用数组记录每个引用的值有几篇文章

之前的写的没保存,贴一个Java的

public int hIndex(int[] citations) {
	int L = citations.length;
	if(L<1) return 0;
	int[] counts = new int[L+1];
	for(int i : citations) {
		if(i>L) counts[L]++;   //hIndex <= length
		else counts[i]++;
	}
	int res = 0;
	for(int k=L; k>=0; k--) {    //采用累加的方式计算引用次数,这样就不需要每次更新比自己大的引用数
	    res += counts[k];
	    if(res>=k) return k;
	}
	return 0;
}


解法2:利用快排的思想,每次迭代选择一个枢纽点,使得数组的左侧小于枢纽点,右侧大于枢纽点。如果枢纽点的值大于右侧的数,则尝试对左边迭代找更大的h-index;反之,则在右边找满足条件的hIndex.

    int partition(vector<int>& citations, int left, int right){
        int mid = (left + right) / 2;   //pivot的选择也可以是其他的,对于随机数组而言,选第一个也可以
        int pivot = citations[mid];
        swap(citations[mid], citations[right]);
        int i = left - 1, j = right;
        for (;;){
            while (citations[++i] < pivot);
            while (j > 0 && citations[--j] > pivot);   //median3的快排针对长度大于2的数组;这里要加入j的条件防止越界
            if (i < j) swap(citations[i], citations[j]);
            else break;
        }
        swap(citations[i], citations[right]);
        return i;
    }

    int hIndex(vector<int>& citations) {
        int n = citations.size();
        int hIndex = 0;
        int left = 0, right = n - 1;
        while (left <= right){
            int index = partition(citations, left, right);
            if (n - index <= citations[index]){   // n-index篇文章有至少n-index次引用
                hIndex = n-index;
                right = index - 1;   //试图找更大的hIndex
            }
            else left = index + 1;
        }
        return hIndex;
    }


H-Index II

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

    int hIndex(vector<int>& citations) {
        int n = citations.size();
        int left = 0;
        int right = n - 1;
        int hIndex = 0;
        while (left <= right){
            int half = (left + right) / 2;
            if (citations[half] >= n - half){
                hIndex = n - half;
                right = half - 1;
            }
            else left = half + 1;
        }
        return hIndex;
    }

在充满仪式感的生活里,一款能传递心意的小工具总能带来意外惊喜。这款基于Java开发的满屏飘字弹幕工具,正是为热爱生活、乐于分享的你而来——它以简洁优雅的视觉效果,将治愈系文字化作灵动弹幕,在屏幕上缓缓流淌,既可以作为送给心仪之人的浪漫彩蛋,也能成为日常自娱自乐、舒缓心情的小确幸。 作为程序员献给crush的心意之作,工具的设计藏满了细节巧思。开发者基于Swing框架构建图形界面,实现了无边框全屏显示效果,搭配毛玻璃质感的弹幕窗口与圆润边角设计,让文字呈现既柔和又不突兀。弹幕内容精选了30条治愈系文案,从“秋天的风很温柔”到“你值得所有温柔”,涵盖生活感悟、自我关怀、浪漫告白等多个维度,每一条都能传递温暖力量;同时支持自定义修改文案库,你可以替换成专属情话、纪念文字或趣味梗,让弹幕更具个性化。 在视觉体验上,工具采用柔和色调生成算法,每一条弹幕都拥有独特的清新配色,搭配半透明渐变效果与平滑的移动动画,既不会遮挡屏幕内容,又能营造出灵动治愈的氛围。开发者还优化了弹幕的生成逻辑,支持自定义窗口大小、移动速度、生成间隔等参数,最多可同时显示60条弹幕,且不会造成电脑卡顿;按下任意按键即可快速关闭程序,操作便捷无负担。 对于Java学习者而言,这款工具更是一份优质的实战参考。源码完整展示了Swing图形界面开发、定时器调度、动画绘制、颜色算法等核心技术,注释清晰、结构简洁,哪怕是初学者也能轻松理解。开发者在AI辅助的基础上,反复调试优化细节,解决了透明度控制、弹幕碰撞、资源占用等多个问题,这份“踩坑实录”也为同类项目开发提供了宝贵经验。 无论是想给喜欢的人制造浪漫惊喜,用满屏文字传递心意;还是想在工作间隙用治愈文案舒缓压力,或是作为Java学习的实战案例参考,这款满屏飘字弹幕工具都能满足你的需求。它没有复杂的操作流程,无需额外配置环境,下载即可运行,用最纯粹的设计传递最真挚的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值