Best Time to Buy and Sell Stock II--疑惑
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--疑惑的更多相关文章
- [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 ...
- 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 ...
- 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- ...
- 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 ...
- 【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 ...
- 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: 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 ...
- 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 ...
- 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 ...
随机推荐
- MySQL获取字段的片段
如表中有很多这样的数据: TEST-123,TEST-III 这种以 TEST开头的数据,为了统计其总数 可以使用mysql自带的方法 substring_index()方法 第一个参数是列的内容, ...
- input输入时软键盘回车显示搜索
很多时候我们会看到输入法里面回车是搜索等字眼,今天我们就来实现这个效果 原本的效果: <form action=""> <input type="sea ...
- Bootstrap使用模态框modal实现表单提交弹出框
Bootstrap 模态框(Modal)插件 模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等.如果 ...
- deblurGAN
-- main.py -- util.py -- data_loader.py -- mode.py -- DeblurGAN.py -- vgg19.py -- layer.py -- vgg19. ...
- JVM(二) 对象存活判断和垃圾回收算法
对象的创建 概述 下面简要介绍创建对象的几个重要步骤 : 检查能否在常量池定位到一个类的符号引用,并检查这个符号代表的类是否已被加载,解析和初始化过.如果没有则执行类加载的操作.(即是说对象的引用放在 ...
- linux修改用户名和密码
linux修改用户名和密码 修改root密码:sudo passwd root 修改用户密码(如hadoop) sudo passwd hadoop 修改主机名:sudo vi /etc/hostna ...
- Spring相关概念的理解理解
spring 框架的优点是一个轻量级比较简单易学的框架,实际使用中的有点优点有哪些呢!1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3 ...
- 前端(一):html标签
HTML(Hypertext Markup Language)超文本标记语言,它负责页面的结构.超文本指的是超链接,使用超链接可以从一个页面跳转到另一个页面. HTML的发展:1993年6月发布第一个 ...
- java温故而知新(9)OOP(面向对象编程)理念
Object Oriented Programming (面向对象的程序设计) 1.定义 面向对象程序设计(OOP)的具体定义很难下,也很容易因此而引起争论,在 Object-Orie ...
- css之子元素获取(未定义高度)父元素的高度
你可能碰到过这样的需求,一个高度不固定的区域(内容由用户创造),当鼠标经过该区域或者其神马操作时,需要出现一个与该区域一样大的模版: 我们用一个span来处理这个mask.由于 .sample-1 和 ...