!!!!!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).
=======================
股票交易问题II
题目:与I不同的地方是,可以做尽可能做的交易,
这一次你可以买一只股票,卖一只股票多次;但是你不能在同一时间进行多次交易,
就是说你必须在你买股票之前,卖掉所有到的股票。
[]
=========
code:
class Solution {
//这一次你可以买一只股票,卖一只股票多次;但是你不能在同一时间进行多次交易,
//就是说你必须在你买股票之前,卖掉所有的股票
public:
int maxProfit(vector<int>& prices) {
//举个例子[5,1,4,5,3,4,5]
// [0,3,1,0,1,1]
//j就是说所有能赚钱的机会,你都能抓住
//而不是只能作一次交易,只能赚5-1=4
int length = prices.size();
int total = ;
for(int i = ;i<length;i++){
if(prices[i]>prices[i-]){
total += prices[i]-prices[i-];
}
}//
return total;
}
};
!!!!!122. Best Time to Buy and Sell Stock II的更多相关文章
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 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(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 ...
- 【一天一道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 ...
- 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 ...
随机推荐
- JavaScipt 源码解析 Sizzle选择器
jQuery的定位就是一个DOM的操作库,那么可想而知选择器是一个至关重要的模块.Sizzle,作为一个独立全新的选择器引擎,出现在jQuery 1.3版本之后,并被John Resig作为一个开源的 ...
- css —— 图片环绕+首行缩进
1.利用css实现图片环绕文字的效果: 只需要给img标签设置float:left/right即可: 2.实现上段文字首行缩进两个字的效果: 使用 text-indent: 2em;即可(em为相对单 ...
- JavaEE SSH框架整合(四) 日志处理Spring结合 log4j、slf4j [转]
1. 加入log4j和slf4j的jar包 2. web.xml: <context-param> <!--log4j配置地址 --> <param-name>lo ...
- Visual Studio 拓展插件——Image Optimizer
一句话概括效用:在Visual Studio的解决方案中,为图片或包含图片的文件夹添加右键菜单,可对图片进行压缩,无损压缩. 在VS扩展工具中安装 安装好后在VS资源管理器中选择图片右键,在右键菜单中 ...
- Sharepoint的javascript客户端对象模型获取其他站点的list
获取当前站点(子站点而不是站点集)下的list var clientContext = new SP.ClientContext.get_current(); var list=clientConte ...
- UVa 1626 Brackets sequence (动态规划)
题意:用最少的括号将给定的字符串匹配,输出最优解.可能有空行. 思路:dp. dp[i][j]表示将区间i,j之间的字符串匹配需要的最少括号数,那么 如果区间左边是(或[,表示可以和右边的字符串匹配, ...
- highcharts异步获取数据
页面异步代码 $(function () { var chart_validatestatics; $(document).ready(function () { var options_valida ...
- Modbus工业协议在Android中的应用
现在工业信息画发展,很多工厂都需要做信息化展示,通常都是利用Android一体机来进行展示和交互. Modbus协议是全球第一个用于工业现场的总线协议,与外设交互可以采用串口通信,tcp等方式:通常在 ...
- js 轮播图代码
js代码 (function(){ /** parent //父容器 changeTime //每次间隔几秒切换下一条 leaveTime //鼠标从小图上离开过后几秒继续切换 index //从第几 ...
- 浅谈C#中的接口和抽象类
C#中接口为"其他方面互不相干"的类型提供公共的服务和特征:C#中class只支持但继承,使用接口却支持多继承,例如:C#中System.String是从System空间中的4个i ...