Leetcode274

题目

274. H 指数

思路

题目有点绕,最开始就想到了对数组进行排序,那就可以通过下标来得到有多少篇论文的引用大于等于当前论文的引用,如果大于了当前论文的引用量则将当前论文的引用量更新为h值。后面考虑到[3,3]这种情况,也就是论文的引用量为3 但是只发表了两篇论文,h指数应该为2,才发现 直接将当前论文的引用量和大于该引用量的论文数量中较小值来更新h值。

代码

java 复制代码
public int hIndex(int[] citations) {
        int n = citations.length;
        // 只有一个值的时候 当论文引用量>1 则h指数为1 否则为0
        if(n <= 1){
            if(citations[0] >= 1){return 1;}
            return 0;
        }
        // 对其进行排序
        Arrays.sort(citations);
        int h = 0;
        for (int i = 0; i < n; i++) {
            int cite = citations[i];
            // 排在后面的论文引用都大于目前引用值 也就是至少有upH篇论文的引用次数>= 当前引用值
            int upH = n - i;
            // 更新h的值为当前引用值 和 引用大于该论文的论文数量 之间的较小值
            h = Math.max(h, Math.min(cite, upH));
        }
        return h;
    }

题目

15. 三数之和

思路

最开始使用暴力,但是没办法去掉重复的,排序后跳过与上一个值相同的数进行暴力,会超时;看了题解,先确定第一个数,然后第三个数从最后一个位置(也就是最大值) 往前枚举,第二个数从第一个数开始向后枚举,三者之和大于0时,最后一个数向前走,当最后一个数与第二个数相同时,表示当前确定的第一个数字 与最小的两个数的和都大于0,第一个数往后继续枚举;

代码

java 复制代码
class Solution {
    // public List<List<Integer>> threeSum(int[] nums) {
    //     Arrays.sort(nums);
    //     List<List<Integer>> re = new ArrayList<>();
    //     int len = nums.length;
    //     // 先确定第一个数
    //     for (int i = 0; i <= len - 3; i++) {
    //         // 需要跳开重复值
    //         if(i > 0 && nums[i] == nums[i-1]){
    //             continue;
    //         }
    //         int temp_a = nums[i];
    //         int target_b = 0 - temp_a;
    //         // 确定第二个数
    //         for (int j = i + 1; j <= len - 2; j++) {
    //             if(j > i + 1 && nums[j] == nums[j-1]){
    //                 continue;
    //             }
    //             int temp_b = nums[j];
    //             int target = target_b - temp_b;
    //             // 找第三个数
    //             for (int k = j + 1; k < len ; k++) {
    //                 if(k > j + 1 && nums[k] == nums[k-1]){
    //                     continue;
    //                 }
    //                 if(nums[k] == target){
    //                     List<Integer> temp = new ArrayList<>();
    //                     temp.add(temp_a);
    //                     temp.add(temp_b);
    //                     temp.add(target);
    //                     re.add(temp);
    //                 }
    //             }
    //         }
    //     }
    //     return re;
    // }
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> re = new ArrayList<>();
        int len = nums.length;
        // 先确定第一个数
        for (int i = 0; i <= len - 3; i++) {
            // 需要跳开重复值
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            int temp_a = nums[i];
            int k = len - 1;
            // 确定第二个数
            for (int j = i + 1; j <= len - 2; j++) {
                if(j > i + 1 && nums[j] == nums[j-1]){
                    continue;
                }
                int temp_b = nums[j];
                // 保证第三个数在第二个数的右边
                while (j < k && temp_a + temp_b + nums[k] > 0){
                    k--;
                }
                if( k == j){
                    break;
                }
                if(temp_a + temp_b + nums[k] == 0){
                    List<Integer> list = new ArrayList<Integer>();
                    list.add(temp_a);
                    list.add(temp_b);
                    list.add(nums[k]);
                    re.add(list);
                }
            }
        }
        return re;
    }
}
相关推荐
syzyc15 分钟前
[ABC267F] Exactly K Steps
数据结构·动态规划·题解
Owen_Q1 小时前
Leetcode百题斩-二分搜索
算法·leetcode·职场和发展
草莓熊Lotso2 小时前
【数据结构初阶】--顺序表(二)
c语言·数据结构·经验分享·其他
汤姆爱耗儿药2 小时前
数据结构——散列表
数据结构·散列表
zstar-_2 小时前
Claude code在Windows上的配置流程
笔记·算法·leetcode
秋说2 小时前
【PTA数据结构 | C语言版】出栈序列的合法性
c语言·数据结构·算法
hi0_63 小时前
03 数组 VS 链表
java·数据结构·c++·笔记·算法·链表
ChoSeitaku3 小时前
NO.3数据结构栈和队列|顺序栈|共享栈|链栈|顺序队|循环队列|链队|双端队列|括号匹配|中缀表达式转后缀|后缀表达式求值
数据结构·microsoft
皮卡蛋炒饭.5 小时前
数据结构—排序
数据结构·算法·排序算法
??tobenewyorker6 小时前
力扣打卡第23天 二叉搜索树中的众数
数据结构·算法·leetcode