LeetCode 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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
public class Solution {
public int maxProfit(int[] prices) {
int profile = 0;
if(prices.length <= 1){
return profile;
}else{
for(int i=1; i<prices.length; i++){
profile += Math.max(prices[i] - prices[i - 1], 0);
}
return profile;
}
}
}
LeetCode 122的更多相关文章
- 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 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 ...
- [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 ...
- Java实现 LeetCode 122 买卖股票的最佳时机 II
122. 买卖股票的最佳时机 II 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意: ...
- leetcode 122 123 309 188 714 股票买卖 动态规划
这类问题有一个通法 https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/solution/yi-ge-tong-y ...
- leetcode 122. Best Time to Buy and Sell Stock II ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java [Leetcode 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 ...
- [LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)
问题 假设你有一个数组,其中的第i个元素表示一只股票在第i天的价格. 设计一个算法找出最大的利润值.你可以进行任意多次的交易(即多次的卖出并买入一份股票).你不能在同一时间进行多次交易(即你必须在再次 ...
- LeetCode 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 ...
随机推荐
- Maven学习总结(一)——Maven入门
原博文出自于:http://www.cnblogs.com/xdp-gacl/p/3498271.html 感谢! 一.Maven的基本概念 Maven(翻译为"专家"," ...
- fx-experience-tools
http://fxexperience.com/2012/03/announcing-fx-experience-tools/ I have some cool new stuff for you t ...
- FIREDAC连接MSSQL 2000报不能支持连接MSSQL2000及更低版本的解决办法
FIREDAC连接MSSQL 2000的时候会报错,原因是MSSQL CLIENT11或MSSQL CLIENT10客户端驱动程序已经不支持连接MSSQL2000及更低版本的数据库. 解决办法: 设置 ...
- ios页面传值的几种方法
1.属性2.方法3.代理方法4.SharedApplication5.NSUserdefault6.通过一个单例的class来传递 属性这种方法传值挺方便的,只需要拿到它的指针,如果重新声明一个指针, ...
- [5] Zygote
Android设备中的两大进程,如下图 1,由init进程创建的Daemon进程 2,由 Zygote进程创建的应用程序进程 什么是Zygote? zygote是“受精卵”的意思.在Android里, ...
- 【PYTHON】二维码生成
二维码是什么? 二维码从一维码扩展而来,增加另一维具有可读性的条码,用黑白矩形图形表示二进制数据,被设备扫描后获取其中包含的信息,二维码的长度.宽度均记载着数据,二维码具有定位点和容错机制,即便没有辨 ...
- android 对一个合并后的联系人选择编辑,手机屏幕会缓慢变暗后再进入编辑界面的问题
1. 手机上有一个合并过的联系人 2. 编辑合并后的联系人 3. 手机屏幕会缓慢变暗之后再进入编辑界面. 首先找到contacts源码包下的EditContactA ...
- Apache POI 合并单元格
合并单元格所使用的方法: sheet.addMergedRegion( CellRangeAddress cellRangeAddress ); CellRangeAddress 对象的构造 ...
- java对象转JSON JS取JSON数据
JsonConfig config = new JsonConfig(); config.setJsonPropertyFilter(new PropertyFilter() { @Override ...
- [小技巧]設定Reporting Services 2008 發生報表管理員權限不足
转载 http://www.dotblogs.com.tw/dorlis.tsao/archive/2011/01/17/20860.aspx 在自己Windows 7 professional的電腦 ...