代码随想录二刷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 分钟前
【数据结构】红黑树详解
数据结构·红黑树
炸膛坦客1 分钟前
嵌入式 - 数据结构与算法:(1-9)数据结构 - 队列(Queue)
c语言·数据结构
~|Bernard|29 分钟前
二.go语言中map的底层原理(2026-5-8)
算法·golang·哈希算法
mzhan01736 分钟前
Linux: compare的直观性
java·linux·服务器
AbandonForce37 分钟前
哈希表(HashTable,散列表)个人理解
开发语言·数据结构·c++·散列表
mask哥41 分钟前
力扣算法java实现汇总整理(下)
java·算法·leetcode
代码中介商42 分钟前
栈结构完全指南:顺序栈实现精讲
c语言·开发语言·数据结构
小陈的进阶之路1 小时前
Python系列课(2)——判断
java·前端·python
刚子编程1 小时前
C# Join 进阶:GroupJoin、性能对决与自定义比较器
java·servlet·c#·join
漫随流水1 小时前
IDEA快速生成构造方法(空参、带参)
java·intellij-idea