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;
    }
}
相关推荐
乌鸦94440 分钟前
《数据结构之美--二叉树》
数据结构·#二叉树
蒟蒻小袁1 小时前
力扣面试150题--K 个一组翻转链表
leetcode·链表·面试
许_安2 小时前
leetcode刷题日记——两数相加
算法·leetcode·职场和发展
泽02023 小时前
数据结构之排序
数据结构·算法·排序算法
Brookty4 小时前
【数据结构】二叉搜索树
数据结构
关于不上作者榜就原神启动那件事4 小时前
力扣hot100,739每日温度(单调栈)详解
算法·leetcode·职场和发展
小卡皮巴拉4 小时前
【力扣刷题实战】丢失的数字
c++·算法·leetcode·位运算
杜小暑4 小时前
数据结构之顺序表
c语言·数据结构
代码程序猿RIP5 小时前
【C语言干货】回调函数
c语言·开发语言·数据结构·c++·算法
刚入门的大一新生5 小时前
C++初阶-模板初阶
开发语言·数据结构·c++