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;
    }
}
相关推荐
iAkuya19 分钟前
(leetcode)力扣100 46二叉树展开为链表(递归||迭代||右子树的前置节点)
windows·leetcode·链表
程序员-King.1 小时前
day151—双端队列—找树左下角的值(LeetCode-513)
算法·leetcode·二叉树·双端队列·队列
苦藤新鸡1 小时前
15 .数组右移动k个单位
算法·leetcode·动态规划·力扣
氷泠2 小时前
路径总和系列(LeetCode 112 & 113 & 437 & 666)
leetcode·前缀和·深度优先·路径总和
橘颂TA2 小时前
【剑斩OFFER】算法的暴力美学——力扣 130 题:被围绕的区域
算法·leetcode·职场和发展·结构与算法
一分之二~2 小时前
回溯算法--解数独
开发语言·数据结构·c++·算法·leetcode
不如语冰3 小时前
AI大模型入门1.1-python基础-数据结构
数据结构·人工智能·pytorch·python·cnn
未来之窗软件服务3 小时前
计算机等级考试—哈希线性探测解答—东方仙盟
数据结构·哈希算法·散列表·计算机软考·仙盟创梦ide·东方仙盟
苦藤新鸡3 小时前
18.矩阵同行同列全置零
数据结构·c++·算法·力扣
程序员-King.3 小时前
day154—回溯—分割回文串(LeetCode-131)
算法·leetcode·深度优先·回溯