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 ...
随机推荐
- Redis+Lua实现限流
相比Redis事务来说,Lua脚本有以下优点减少网络开销: 不使用 Lua 的代码需要向 Redis 发送多次请求, 而脚本只需一次即可, 减少网络传输;原子操作: Redis 将整个脚本作为一个原子 ...
- Kubernetes Running Locally
1. Requirements 1) Linux Not running Linux? Consider running Minikube, or on a cloud provider like G ...
- 再探php
1. 如何打开一个php文件? 启动本地服务器和MySQL, 然后将php文件放在xampp -> htdocs 目录下(可以是子目录.孙子目录 ......),打开浏览器,在浏览器中输入 l ...
- LinuxShell脚本基础 6-case...esac的使用和通配符
1.case...esac的使用 #!/bin/bash echo "请输入编号 选择不同的显示文件和目录方式:" echo "1 - 普通显示" echo & ...
- Android学习系列--App列表之拖拽ListView(下)
接着上篇Android学习系列(10)--App列表之拖拽ListView(上)我们继续实现ListView的拖拽效果. 7.重写onTouchEvent()方法. 在这个方法中我们主要是处理 ...
- Android ListView分组显示
ListView的实现方法也是普通的实现方法.只不过在list列表中加入groupkey信息.在渲染的时候要判断是否是分组的标题. 就是在使用不同的两个View的时候存在这种情况,convertVie ...
- Log4J2 配置文件模板及代码说明
Log4j是Apache的著名项目,随着Java应用的越来越广泛,对日志性能等方面的要求也越来越高.Log4j的升级版本Log4j2在前些年发布.Log4J2的优点和好处有很多,可以自行搜索查阅相关文 ...
- httpclient文件下载
http://blog.csdn.net/nupt123456789/article/details/42721003
- 通过管道传输快速将MySQL的数据导入Redis
通过管道传输pipe将MySQL数据批量导入Redis 自Redis 2.6以上版本起,Redis支持快速大批量导入数据,即官网的Redis Mass Insertion,即Pipe传输, ...
- 记一次走心One 2 One沟通
聊的比较零零碎碎,内容比较散,有些solution不错,记一些要点吧(1)要学会汇报,就是坐你身边的人,也未必知道你在干啥 三个人都在砌墙.当人们分别问他们在做什么,他们的答案却不一样:第一个人头也没 ...