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. oracle--块信息深入解析

    一,创建 Data Block是数据库中最小的I/O单元 01,建立一个新的表空间 查看默认表空间位置 select TABLESPACE_NAME,FILE_NAME from dba_data_f ...

  2. JS及Dom练习 | 页面滚动文字

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. (转)expect命令脚本语言介绍及生产实践

    原文:http://www.fblinux.com/?p=526 Expect介绍 expect是一个用来实现自动交互功能的软件套件,是用来实现自动和交互式任务程序进行通信,无需人的手工干预.比如SS ...

  4. Nodejs 实现windows后台运行

    首先需要到http://nssm.cc/download/?page=download 下载 nssm 下下来之后是压缩包形式的,解压之后 ctrl + R 进入cmd 命令行界面 在命令行模式下进入 ...

  5. document.referrer的使用和window.opener 跟 window.parent 的区别

    偶尔看到了document.referrer,之前一直有点疑惑与window.opener 和 window.parent之间的区别 首先查了一下w3cSCHOOL, 上面的解释:referrer 属 ...

  6. lua三目运算符

    lua的类似三目运算符用法 一般化的Lua三目运算为:(a and {b} or {c})[1] local v = (a and {b} or {c})[1]如果a为true,则 v = b 如果a ...

  7. Linux 信号signal处理函数--转

    alarm(设置信号传送闹钟)相关函数 signal,sleep 表头文件 #include<unistd.h> 定义函数 unsigned int alarm(unsigned int ...

  8. Java入门系列-14-深入类和对象

    这篇文章用大量的代码帮你搞懂:值传递和引用传递.构造方法.方法重载.static关键字的使用 方法参数传递-值传递和引用传递 1.值传递 敲一敲: /** * 使用方法交换两个变量的值 * @auth ...

  9. PHP代码语法验证插件:SublimeLinter

    1.调出控制台 Ctrl+Shift+P,输入 install package,Enter

  10. sublime text 3 PHP 所需插件

    1. PHP代码语法验证插件:SublimeLinter 2.Bracket Highlighter 用于匹配括号,引号和html标签.对于很长的代码很有用.安装好之后,不需要设置插件会自动生效 3. ...