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;
    }
}
相关推荐
苏宸啊1 天前
链式二叉树基操代码实现&OJ题目
数据结构
风筝在晴天搁浅1 天前
hot100 25.K个一组翻转链表
数据结构·链表
小十一再加一1 天前
【初阶数据结构】栈和队列
数据结构
长安er1 天前
LeetCode136/169/75/31/287 算法技巧题核心笔记
数据结构·算法·leetcode·链表·双指针
_w_z_j_1 天前
最小栈(栈)
数据结构
fufu03111 天前
Linux环境下的C语言编程(四十八)
数据结构·算法·排序算法
思成Codes1 天前
数据结构:基础线段树——线段树系列(提供模板)
数据结构·算法
卜锦元1 天前
Golang后端性能优化手册(第三章:代码层面性能优化)
开发语言·数据结构·后端·算法·性能优化·golang
Binky6781 天前
力扣--回溯篇(2)
算法·leetcode·职场和发展
2401_841495641 天前
【LeetCode刷题】打家劫舍
数据结构·python·算法·leetcode·动态规划·数组·传统dp数组