代码随想录二刷day32

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣122. 买卖股票的最佳时机 II](#一、力扣122. 买卖股票的最佳时机 II)
  • [二、力扣55. 跳跃游戏](#二、力扣55. 跳跃游戏)
  • [三、力扣45. 跳跃游戏 II](#三、力扣45. 跳跃游戏 II)

前言


一、力扣122. 买卖股票的最佳时机 II

java 复制代码
class Solution {
    public int maxProfit(int[] prices) {
        int cur = prices[0], res = 0;
        for(int i = 1; i < prices.length; i ++){
            if(prices[i] > cur){
                res += (prices[i] - cur);
                cur = prices[i];
            }else{
                cur = prices[i];
            }
        }
        return res;
    }
}

二、力扣55. 跳跃游戏

java 复制代码
class Solution {
    public boolean canJump(int[] nums) {
        if(nums.length == 1){
            return true;
        }
        int scope = 0;
        for(int i = 0; i <= scope; i ++){
            scope = Math.max(scope, i + nums[i]);
            if(scope >= nums.length - 1){
                return true;
            }
        }
        return false;
    }
}

三、力扣45. 跳跃游戏 II

java 复制代码
class Solution {
    public int jump(int[] nums) {
        int result = 0;
        // 当前覆盖的最远距离下标
        int end = 0;
        // 下一步覆盖的最远距离下标
        int temp = 0;
        for (int i = 0; i <= end && end < nums.length - 1; ++i) {
            temp = Math.max(temp, i + nums[i]);
            // 可达位置的改变次数就是跳跃次数
            if (i == end) {
                end = temp;
                result++;
            }
        }
        return result;
    }
}
相关推荐
艺杯羹1 分钟前
AI编程时代软件工程怎么学:从底层思维认知到驱动智能体的架构跃迁
java·人工智能·ai·架构·软件工程·ai编程
土司大王4 分钟前
LeetCode hot100——51.N 皇后:Java 回溯模板、列与对角线剪枝、O(n!) 复杂度分析
java·leetcode·剪枝
重生之小比特17 分钟前
【Java SE】IDEA 调试 Debug 案例完整分析
java·ide·intellij-idea
晴天1618 分钟前
JavaScript 与 Java 垃圾回收机制对比
java·开发语言·javascript
金金计较.20 分钟前
Go语言-2
开发语言·算法·golang
xiaohua100928 分钟前
JVM内存优化-jemalloc
java·jvm
计算机编程-吉哥31 分钟前
基于机器学习的城市交通拥堵分析与预测平台【计算机毕业设计选题·机器学习·随机森林算法】
hadoop·算法·随机森林·机器学习·课程设计·计算机毕业设计选题·大数据毕业设计选题推荐
吃饱了得干活32 分钟前
Redisson 进阶实战:看门狗机制与分布式锁完全指南
java·redis·后端
淘源码d40 分钟前
基于Java开发的上门家政系统源码,采用SpringBoot+MySQL+UniApp+Vue3技术栈,构建三端一体化平台
java·源码·上门家政·家政系统·同城家政·源码部署
Wang's Blog42 分钟前
Java框架快速入门: Spring Security+OAuth2之多因子认证逻辑与实体类改造
java·开发语言·spring