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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  5. 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 ...

  6. 【刷题-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 ...

  7. 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 ...

  8. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. java 序列化原来如此

    上次面试的时候 ,如何实现java 类的序列化,当时感觉这个问题很简单,我的回答是实现serizlizable 接口就好了,可以实现对象的持久化,看了看书,原来这样: public class Ser ...

  2. 一、Vue CLI

    一.Vue CLI https://cli.vuejs.org/zh/guide/installation.html 介绍: 二.安装 # 安装 Vue Cli npm install -g @vue ...

  3. 问题:关于2.3 jmu-Java-02基本语法-03-身份证排序 (9 分)

    输出未能排序     import java.util.Scanner;     import java.util.Arrays;     public class Main {         pu ...

  4. WPF 多个选项卡TabControl 页面分离

    此项目源码下载地址:https://github.com/lizhiqiang0204/TabControl-page-separation 每个页面的按键处理事件直接对应该页面下的cs文件 Main ...

  5. 上传base64图片到七牛云前端遇到的坑

    介意前端普通引入七牛云SDk上传图片到七牛云需要多个js,所以才有了base64的上传方式,简化操作,(懒.) 七牛云官方文档如下 https://developer.qiniu.com/kodo/k ...

  6. pyqt5-QFrame边框样式

    继承  QObject-->QWidget-->QFrame 是一个基类, 可以选择直接使用,主要是用来控制一些边框样式:例如:凸起.凹下.阴影.线宽  QFrame对象效果对照图: im ...

  7. SpringCloud学习系列-Eureka服务注册与发现(1)

    1.Eureka的基本架构 Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper). Eureka 采用了 C-S 的设计架构 ...

  8. vs2017 制作winfrom 安装包!

    https://www.cnblogs.com/GuZhenYin/p/8176981.html 转载 前言 项目中有用到winfrom做配套的打印程序,直接给客户一个debug文件夹,当然不是很好. ...

  9. 对 Promises/A+ 规范的研究 ------引用

    作为 Modern JavaScript 基础设施的一部分,Promises 对前端开发者而言异常重要.它是 async/await 语法的基础,是 JavaScript 中处理异步的标准形式.并且, ...

  10. 上采样 及 Sub-pixel Convolution (子像素卷积)

    参考:https://blog.csdn.net/leviopku/article/details/84975282 参考:https://blog.csdn.net/g11d111/article/ ...