leetcode-easy-array-122 best time to buy and sell stocks II
mycode 69.45%
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
res = 0
for i in range(1,len(prices)):
temp = prices[i] - prices[i-1]
if temp <= 0:
continue
else:
res += temp
return res
参考:
下面的更快,因为索引查找的次数少一些!
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices) == 0:
return 0
else:
profit = 0
start = prices[0]
for i in range(1,len(prices)):
if prices[i]>start:
profit += prices[i] - start
start = prices[i]
else:
start = prices[i]
return profit
leetcode-easy-array-122 best time to buy and sell stocks II的更多相关文章
- [LeetCode&Python] Problem 122. Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [Array]122. Best Time to Buy and Sell Stock II(obscure)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- leetcode:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 【刷题-LeetCode】122 Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- 122. Best Time to Buy and Sell Stock II@python
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- docker无法删除<none>镜像
.进入root权限 sudo su # 或 sudo -i .停止所有的container(这样才能够删除其中的images): docker stop $(docker ps -a -q) 如果想要 ...
- java hashmap&concurrentHashmap源理
Java集合:HashMap底层实现和原理(源码解析) https://www.cnblogs.com/java-jun-world2099/p/9258605.html HashMap源码解析JDK ...
- uoj #450[集训队作业2018]复读机
传送门 \(d=1\),那么任何时刻都可以\(k\)个复读机的一种,答案为\(k^n\) \(d>1\),可以枚举某个复读机的复读次数(必须是\(d\)的倍数),然后第\(i\)个复读时间为\( ...
- 3-关于ES的几个小疑问和解答
1.ES如何实现分布式 2.ES如何实现高实时 3.ES如何实现高扩展 4.ES7.x版本为何废弃type 5.搜索原理--知乎es
- php前端做过滤校验
http://www./test_form.php/%22%3E%3Cscript%3Ealert('hacked')%3C/script%3E 以上的 URL 中,将被解析为如下代码并执行: < ...
- layui ri laydate的常规使用,并且日期最大不能超过当前日期
laydate的常规使用,分为两种方式实现日期组件 一.在 layui 模块中使用 下载layui 地址 :https://www.layui.com/ 引入资源路径 js 和 css 通过下面 ...
- Wiki 安装部署
#首先登陆进入 MySQL 数据库 [root@oldboy tools]# mysql -uroot -poldboy123 #创建一个 wiki 是库 mysql> create datab ...
- Java学习03-进制学习
计算机中是以二进制来进行数据传递的,二进制分为二进制.八进制.十进制.十六进制 而他们之间如何进行转换呢,二进制作为元,其他进制都是经二进制进行换算的,所以无论什么进制之间的转换都是先转换为二进制,再 ...
- jquery 保留两位小数
jquery 通过 toFixed 保留两位小数 <script> var num = 12.21654; console.log(num.toFixed(2)) </script& ...
- Spring Boot JPA - Querydsl
https://lufficc.com/blog/spring-boot-jpa-querydsl