代码随想录二刷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;
    }
}
相关推荐
dadaobusi3 小时前
学习:RV lkvm SBI
java·学习·spring
HEJOO95 小时前
深入理解 Java volatile 关键字:原理、用法与常见误区
java·开发语言·spring
曹牧5 小时前
Java:no content to map due to end of input
java·开发语言
阿维的博客日记5 小时前
Example 类全场景实战指南
java·mybatis
两点王爷6 小时前
SpringBoot 项目集成 GeoServer 源码,实现地图服务发布
java·spring boot·后端
Javatutouhouduan6 小时前
Java如何速通性能优化难题?
java·java面试·jvm调优·后端开发·java程序员·java八股文·java性能优化
2601_967760786 小时前
2026年PDF压缩与页码添加工具技术实测:性能、算法与本地化适配深度对比
算法·pdf
lemon_sjdk6 小时前
DOM 节点
java·前端·javascript
leoZ2317 小时前
2026-09-09-springboot-cloud-deploy-pitfalls
java·前端·javascript·vue.js·人工智能·spring boot·后端
不会就选b7 小时前
算法日常・每日刷题--<贪心>7
数据结构·算法·leetcode