代码随想录二刷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;
    }
}
相关推荐
phltxy4 小时前
C语言操作符详解
java·c语言·算法
aqiu1111114 小时前
【LeetCode 902】最大为 N 的数字组合 - 详细题解与数位组合思路
数据结构·算法·leetcode·蓝桥杯·数位dp
步行cgn5 小时前
@Configuration 详解:Spring 配置类的核心注解
java·后端·spring
sunshine22 girl5 小时前
Java学习一 环境配置2 安装和基本使用Idea
java·学习·intellij-idea
辰烨chenye5 小时前
LeetCode Hot 100 题解 · 哈希篇
算法·leetcode·哈希算法
罗西的思考6 小时前
DreamZero 与 DreamDojo:世界模型与策略的分层协同综合分析与对比
人工智能·算法·机器学习
玖玥拾6 小时前
LeetCode 219 存在重复元素 II
算法·leetcode·哈希算法·散列表
嘿嘿-666 小时前
Windows 一键使用 GPT-6 Astra:Codex CLI 配置教程
java·人工智能·windows·gpt·chatgpt·web
知无不研7 小时前
c语言中循环的介绍与简单应用
c语言·开发语言·算法·循环·for·while