买卖股票的最大收益 II

122. Best Time to Buy and Sell Stock II (Easy)

题目描述:

  可以进行多次交易,多次交易之间不能交叉进行,可以进行多次交易。

思路分析:

  当访问到一个prices[i]且prices[i]-prices[i-1]>0,那么就把prices[i]-prices[i-1]添加到收益中。

代码:

public int maxProfit(int []prices){
int profit=0;
for(int i=1;i<prices.length;i++){
if(prices[i]>prices[i-1]){
profit=profit+(prices[i]-prices[i-1]);
}
}
return profit;
}

贪心策略---买卖股票的最大收益 II的更多相关文章

  1. 买卖股票的最佳时机I II III IV

    I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ...

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

  3. 贪心——122.买卖股票的最佳时机II

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次 ...

  4. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. lintcode:买卖股票的最佳时机 IV

    买卖股票的最佳时机 IV 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格. 设计一个算法来找到最大的利润.你最多可以完成 k 笔交易. 注意事项 你不可以同时参与多笔交易(你必须在再次 ...

  7. [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. 【LeetCode】122、买卖股票的最佳时机 II

    Best Time to Buy and Sell Stock II 题目等级:Easy 题目描述: Say you have an array for which the ith element i ...

随机推荐

  1. 【改】shell 判断文件中有无特定子串方法(grep)

    转自:https://blog.csdn.net/zhuguiqin1/article/details/79160923 利用grep执行的命令结束代码$?的值来判断是否已经grep到特定的值. 当$ ...

  2. [转]Tomcat9.0安装教程 Tomcat9.0环境变量配置教程

    [转]Tomcat9.0安装教程 Tomcat9.0环境变量配置教程 [转]超详细MySQL安装及基本使用教程

  3. .h与.cpp

    本质上没什么区别. cpp:c plus plus,就表示为c++原文件. .h文件实现的功能是声明.cpp文件中需要使用的变量.函数及宏定义等. .h文件就像是一个接口,具体的实现可以在.cpp中, ...

  4. JSP和Servlet及浏览器与tomcat交互过程

    JSP与SERVLET区别 JSP在本质上就是Servlet,但是两者的创建方式不一样. JSP由HTML代码和JSP标签构成,可以方便地编写动态网页.因此在实际应用中采用Servlet来控制业务流程 ...

  5. Java第二阶段之常用类

    包装类缓存-128到127之间的数字(系统初始的时候就创建了,当我们调用ValueOf时,首先检查是否在范围内,在则直接取用)integer in1 = integer.valueOf(-128):i ...

  6. 如何将数组中的元组包转化为字典通过json序列化给前端

  7. php str_replace()函数 语法

    php str_replace()函数 语法 作用:字符串替换操作,区分大小写大理石构件 语法:str_replace(find,replace,string,count) 参数: 参数 描述 fin ...

  8. navigator.userAgent.toLowerCase();判断浏览器做兼容

    js简单实例: var ua = navigator.userAgent.toLowerCase(); if (/android/.test(ua)) { $('.date>div>img ...

  9. 29 August

    P1352 Bosses' Masquerade 树形DP模板. #include <cstdio> #include <algorithm> using namespace ...

  10. ThreadLocal学习资料

    下面的这一段代码运行起来,就会发生线程安全问题: 启动两个线程,同时去修改 name 属性值. package com.liwei.thread; /** * 下面的代码演示了线程安全发生的由来 * ...