代码随想录二刷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;
    }
}
相关推荐
Raink老师10 小时前
用100道题拿下你的算法面试(矩阵篇-2):求转置矩阵
算法·面试·矩阵
苏瞳儿13 小时前
java对数据库的增删改查
java·数据库·oracle
zhangjw3413 小时前
Java基础语法:变量、数据类型与运算符,从原理到实战
java·开发语言
算法鑫探16 小时前
闰年判断:C语言实战解析
c语言·数据结构·算法·新人首发
yaoxin52112316 小时前
384. Java IO API - Java 文件复制工具:Copy 示例完整解析
java·开发语言·python
WBluuue16 小时前
数据结构与算法:康托展开、约瑟夫环、完美洗牌
c++·算法
NotFound48617 小时前
实战指南如何实现Java Web 拦截机制:Filter 与 Interceptor 深度分享
java·开发语言·前端
木子墨51617 小时前
LeetCode 热题 100 精讲 | 并查集篇:最长连续序列 · 岛屿数量 · 省份数量 · 冗余连接 · 等式方程的可满足性
数据结构·c++·算法·leetcode
2501_9219608518 小时前
双相自指图与弦论边界非对易性的结构同源
数据结构
王老师青少年编程18 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:均分纸牌
c++·算法·编程·贪心·csp·信奥赛·均分纸牌