https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

代码如下时能AC

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

但是,代码如下时却Runtime Error,提示Last executed input:[]

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

这代码明明跟这段Java是一样的啊。这Java代码也能AC。奇怪。

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

最后只能是加入一行

  if(prices.size()==) return ;

来保证edge case.

——————————————————————————————————————————————————————————————————————

This problem is solved by Shangrila finally:

Replace prices.size()-1 by int(prices.size())-1. The type of prices.size() is SIZE_T, which is unsigned integer types. -1 could overflow.

answered 9 hours ago by Shangrila (48,010 points)

Best Time to Buy and Sell Stock II--疑惑的更多相关文章

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

  3. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

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

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

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

  7. LeetCode: Best Time to Buy and Sell Stock II 解题报告

    Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...

  8. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

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

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

随机推荐

  1. centos7 装机配置及 mysql 安装过程

    打开网卡,使操作系统可以上网 1 ip add 查看网卡,lo是回环网卡可以忽略,ens33为实际网卡. [root@localhost ~]# ip add 1: lo: <LOOPBACK, ...

  2. 设置开机自动运行vncserver

    a. 在/etc/rc.d/rc.local文件中加入下面行   /etc/init.d/vncserver startb. 编辑/etc/sysconfig/vncservers   VNCSERV ...

  3. zookeeper 选举机制 和 eruake

    zookeeper简介: 在分布式环境中,多个服务之间协调一致.有提供分布式锁.服务配置.实现分布式领域CAP(consistency一致性,Availiablity高可用,patition tolr ...

  4. (转)nginx日志配置指令详解

    这篇文章主要介绍了nginx日志配置指令详解,nginx有一个非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志,需要的朋友可以参考下日志对于统计排错来说非常有利的.本文总结了nginx日 ...

  5. selenium+Python(生成html测试报告)

    当自动化测试完成后,我们需要一份漂亮且通俗易懂的测试报告来展示自动化测试成果,仅仅一个简单的log文件是不够的 HTMLTestRunner是Python标准库unittest单元测试框架的一个扩展, ...

  6. C 标准库 - string.h之strpbrk使用

    strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...

  7. 使用 Qt 获取 UDP 数据并显示成图片

    一个项目,要接收 UDP 数据包,解析并获取其中的数据,主要根据解析出来的行号和序号将数据拼接起来,然后将拼接起来的数据(最重要的数据是 R.G.B 三个通道的像素值)显示在窗口中.考虑到每秒钟要接收 ...

  8. 周记2——ios的日期格式bug

    转眼又到了周末,转眼又要上班,转眼...大概这就是一眼万年的意思吧. 这周继续IM(即时聊天),项目用的是LayIM移动端改装的,仅仅“借用”了一个聊天窗口.由于是内嵌App的页面,自然少不了Andr ...

  9. TSM简介

    转自:https://www.cnblogs.com/Study-Blog/p/8644376.html Tivoli TSM产品功能详述Tivoli TSM(Tivoli Storage Manag ...

  10. PL/SQL之流控制语句

    1.选择控制语句 --语法1--IF 条件 THEN 语句; END IF; DECLARE v_Salary ,); BEGIN SELECT salary INTO v_Salary FROM a ...