【leetcode-python-27】122. 买卖股票的最佳时机 II
渣渣原始版(44.28%)
每天都决定一下买卖与否。
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
num = 0
for i in range(len(prices)-1):
if prices[i+1] > prices[i]:
num += prices[i+1] - prices[i]
return num
渣渣原始版(53.05%)
先进行操作,再判断。只是简化了代码。
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
num = 0
for i in range(len(prices)-1):
x = prices[i+1] - prices[i]
if x>0:
num += x
return num
新手入坑,多多包涵~~