Say you have an array for which the i th 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).

题意:可以做多次交易,但必须是做完一次交易了,再做下一次。

思路:这个要注意和显示生活中区别,这里是每天股票的价格已经知道了,所以,买卖的时候,只要是盈利的都做。大致过程是,对相邻两天的股票价格比较,只要后者大于前者,这次买卖就做。代码如下:

 class Solution {
public:
int maxProfit(vector<int> &prices)
{
int profit=;
int len=prices.size();
for(int i=;i<len-;++i)
{
if(prices[i+]-prices[i]>)
profit+=prices[i+]-prices[i];
}
return profit;
}
};

这里值得注意的是:若代码中第6、7两行写成如下形式,是不能AC的。个人认为是prices.size()的类型为vector<int>::size_type,而减1以后,在prices.size()=0的情况,会负溢出。导致i小于一个正整数,但是数组中没有值,对数组进行访问时,会发生段问题。

(自己的考虑,没有考虑到类型限制,详情见下面)

 for(int i=;i<prices.size()-;++i)
{
......
}

在本地验证的代码:

 #include<iostream>
#include<vector> using namespace std; int main()
{
vector<int> a ;
cout << a.size() - << endl;
return ;
} //输出42949647295

解决的方法:一、之前加if判断,二、使用最上面代码中的那种方式--先求数组大小,然后用这个值去参与for循环。

//更新

这里给出Grandyang的回答:因为prices.size()返回的是size_type型的,这是一种unsigned类型,也就是无符号类型,所以在直接对其做减法是不行的,因为负数对其来说不合法,所以我们可以先强制将其转为整型,再做法,比如:

 for(int i = ; i < (int)prices.size() - ; ++i)
{
......
}

(感谢Grandyang!!!)

[Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机的更多相关文章

  1. [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机

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

  2. 122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II

    假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票) ...

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

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

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

  5. 123 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III

    假设你有一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易.注意:你不可同时参与多笔交易(你必须在再次购买前出售掉之前的股票).详见: ...

  6. 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV

    假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...

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

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

随机推荐

  1. (转)手游应该怎么做 UI 设计?

    之前一直做互动设计,在UI上有一些积累. 转战手游后发现,有着标准.互动方式.用户行为等等与常规大屏UI设计的不同,但是在设计流程,思考方式上是一样的. 以目前项目中一个界面为例(未完成版本)来说一下 ...

  2. 12.2.1 访问元素的样式【JavaScript高级程序设计第三版】

    任何支持style 特性的HTML 元素在JavaScript 中都有一个对应的style 属性.这个style 对象是CSSStyleDeclaration 的实例,包含着通过HTML 的style ...

  3. keil5 mdk调用外部编辑器notepad++、sublime3、VSCode总结

    1.打开keil主界面,点击菜单栏Tools菜单,选择如下图所示的选项. 2.点击如下图所示的菜单上红笔标注的地方,给这个工具命名,如notepad++.sublime3.vscode等,如下图, 并 ...

  4. array_x

    import java.util.*; public class array_x { public static void main(String args[]) { int a[][]={{2,4, ...

  5. R语言绘图:箱线图

    使用ggplot2绘制箱线图 ######*****绘制箱线图代码*****####### data1$学区房 <- factor(data1$school, levels = 0:1, lab ...

  6. python2.7练习小例子(二十一)

        21):1.题目:两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已抽签决定比赛名单.有人向队员打听比赛的名单.a说他不和x比,c说他不和x,z比,请编程序找出三队 ...

  7. HBase 伪分布式环境搭建及基础命令使用

    一.前提条件: (1)文件存储在HDFS文件系统之上.因此必须启动hadoop服务.(namenode,datanode,resourcemanager,nodemanager,historyserv ...

  8. 设计模式——模版方法模式详解(论沉迷LOL对学生的危害)

    .  实例介绍 在本例中,我们使用一个常见的场景,我们每个人都上了很多年学,中学大学硕士,有的人天生就是个天才,中学毕业就会微积分,因此得了诺贝尔数学奖:也有的人在大学里学了很多东西,过得很充实很满意 ...

  9. (原)MongoDB在系统中的使用

    序)Nosql并不是要取代原有的数据产品,而是为不同的应用场景提供更多的选择. 一)结构类型 传统数据库的领域在于结构化文档,对于非结构化文档和半结构化文档,它能处理,但是有一定的缺陷,那么什么又是结 ...

  10. js学习日记-隐式转换相关的坑及知识

    隐式转换比较是js中绕不过去的坎,就算有几年经验的工程师也很有可能对这块知识不够熟悉.就算你知道使用===比较从而避免踩坑,但是团队其它成员不一定知道有这样或那样的坑,有后端语言经验的人常常会形成一个 ...