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;
    }
}
相关推荐
胡萝卜3.04 小时前
数据结构初阶:排序算法(一)插入排序、选择排序
数据结构·笔记·学习·算法·排序算法·学习方法
lyx33136967594 小时前
Pandas数据结构详解Series与DataFrame
数据结构·pandas
散1125 小时前
01数据结构-交换排序
数据结构·算法
无聊的小坏坏6 小时前
拓扑排序详解:从力扣 207 题看有向图环检测
算法·leetcode·图论·拓扑学
浮灯Foden9 小时前
算法-每日一题(DAY13)两数之和
开发语言·数据结构·c++·算法·leetcode·面试·散列表
执子手 吹散苍茫茫烟波11 小时前
leetcode415. 字符串相加
java·leetcode·字符串
执子手 吹散苍茫茫烟波12 小时前
LCR 076. 数组中的第 K 个最大元素
leetcode·排序算法
山顶风景独好13 小时前
【Leetcode】随笔
数据结构·算法·leetcode
科大饭桶14 小时前
C++入门自学Day11-- String, Vector, List 复习
c语言·开发语言·数据结构·c++·容器
Cx330❀16 小时前
【数据结构初阶】--排序(四):归并排序
c语言·开发语言·数据结构·算法·排序算法