【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;

    }
}
相关推荐
wuminyu13 分钟前
JUC组件逐层剥离与深度剖析
java·linux·c语言·jvm·c++·算法
圣保罗的大教堂32 分钟前
leetcode 3016. 输入单词需要的最少按键次数 II 中等
leetcode
hhzz38 分钟前
机器学习-算法模型系列文章:07-SVM 一巴掌拍出来的超平面:SVM核函数选错,再干净的数据也救不了你
算法·机器学习·支持向量机
geovindu1 小时前
java: Gale-Shapley Algorithm
java·开发语言·后端·算法
冻柠檬飞冰走茶1 小时前
PTA基础编程题目集 7-34 通讯录的录入与显示(C语言实现)
c语言·开发语言·数据结构·算法
zander2581 小时前
LeetCode 79. 单词搜索
算法·深度优先
老洋葱Mr_Onion1 小时前
【C++】高精度模板
开发语言·c++·算法
脚踏实地皮皮晨2 小时前
003003002_WPF Grid 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
aaaameliaaa10 小时前
字符函数和字符串函数
c语言·笔记·算法
城管不管11 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划