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. jumserver 官方文档和

      一步一步安装(CentOS) 本文档旨在帮助用户了解各组件之间的关系, 生产环境部署建议参考 进阶安装文档 安装过程中遇到问题可参考 安装过程中常见的问题 测试推荐环境 CPU: 64位双核处理器 ...

  2. 用代码如何检测一个android程序是否在运行

    /** * 检测一个android程序是否在运行 * @param context * @param PackageName * @return */ public static boolean is ...

  3. proxy写监听方法,实现响应式

    var data = { price: 5, quantity: 2 };var data_without_proxy = data; // 保存源对象data = new Proxy(data_wi ...

  4. 一个数字键盘引发的血案——移动端H5输入框、光标、数字键盘全假套件实现

    https://juejin.im/post/5a44c5eef265da432d2868f6 为啥要写假键盘? 还是输入框.光标全假的假键盘? 手机自带的不用非得写个假的,吃饱没事干吧? 装逼?炫技 ...

  5. MVC流程

    控制器:调用模型,并调用视图,将模型产生数据传递给视图,并让相关视图去显示 模   型:获取数据,并处理返回数据 视   图:是将取得的数据进行组织.美化等,并最终向用户终端输出 第一步 浏览者 -& ...

  6. Git学习系列之Git 的优势有哪些?

    Git 的优势主要有: 1.更方便的 Merge 分布式管理必然导致大量的 Branch 和 Merge 操作.因此分布式版本控制系统都特别注意这方面.在传统的 CVS 里面制作 Branch 和 M ...

  7. frames的对象兼容性获取以及跨域实现数据交换(js文件的加载判断)

    1.document.frames()与document.frames[]的区别 <html> <body> <iframe id="ifr_1" n ...

  8. p2p的UDP打洞原理

    >>>>>>>>>>>>>>>>>>>>>>>>> ...

  9. GraphQL 总结 + 在Django应用(Graphene)

        由Xmind编辑,下次更新会附加python demo.         附件列表

  10. DIV+CSS常见面试题

    1.!important拥有最高的优先级,几乎所有浏览器都支持!important,除了IE6(不完全支持) 例1(IE6支持,颜色为#e00): .cssClass{color:#e00!impor ...