【LeetCode热题100】--121.买卖股票的最佳时机

121.买卖股票的最佳时机

java 复制代码
class Solution {
    public int maxProfit(int[] prices) {
        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;
        for(int i =0;i<prices.length;i++){
            if(prices[i] < minprice){
                minprice = prices[i]; //找到最小值
            }else if(prices[i] - minprice > maxprofit){ //记录之后与最小值之间得差值
                maxprofit = prices[i] - minprice;  
            }
        }
        return maxprofit;

    }
}
相关推荐
Nil20819 分钟前
leetcode 199二叉树的右视图
算法·leetcode·深度优先
M78佐菲1 小时前
c语言学习笔记:排序与查找方法整理
linux·c语言·笔记·学习·算法
AI备案指南-满满3 小时前
人工智能拟人化互动服务安全自评估报告的评估要点有哪些?
人工智能·算法·安全·机器人·大模型备案·算法备案
土司大王5 小时前
LeetCode hot100——两两交换链表中的节点
算法·leetcode·职场和发展
大熊背6 小时前
树莓派IspPipeline LSC模块原理详解
算法·lsc·isppipeline·mesh lsc
zander2586 小时前
LeetCode 300. 最长递增子序列
算法·leetcode·深度优先
欧叶冲冲冲7 小时前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
祖力557 小时前
Linux应用软件编程:目录IO与framebuffer
linux·运维·算法·framebuffer·目录io
名字还没想好☜7 小时前
Go 用 slices/maps 标准库泛型函数:告别手写 Contains、Sort、去重(Go 1.21)
开发语言·后端·算法·golang·go