Best Time to Buy and sell Stock
来源:互联网 发布:淘宝网免费注册 编辑:程序博客网 时间:2024/05/19 06:16
题目详情:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ if len(prices)<=1: #如果元素个数小于等于1个 return 0 #直接放回0 rear=1#循环之前都是进行初始化 front=0 maxP=0#代表最大利润 while rear<len(prices): nowP=prices[rear]-prices[front] #当前的利润,不一定是最大的 if nowP<=0: #如果当前利润小于0,即prices[rear]-prices[front]<=0 front=rear #移项之后为prices[rear]<=prices[front],后边的元素(价格)更小 rear=rear+1#那么将rear赋值给front,rear加1,接着检查后边的元素 if nowP>0:#当前元素大于0 if maxP<nowP: #并且nowP>maxP maxP=nowP#那么更改最大利润的值 rear+=1 return maxP
阅读全文
0 0
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time To Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- 架构师修炼之路-高级技术修炼
- 经验总结
- 使用GPU云服务器加速训练
- 酒客项目日志20170730
- HDU 6055 Regular polygon【几何】
- Best Time to Buy and sell Stock
- Java四种线程池的使用
- PyTorch(五)——PyTorch源码修改之增加ConvLSTM层
- 201312-3 最大的矩形
- NVIDIA Jetson TX1 系列开发教程之八:Qt安装与配置
- 深层揭密extern "C"
- C#发送和接收消息
- srlua编译生成exe可执行文件
- CSS盒子模型