[Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
题意:可以做多次交易,但必须是做完一次交易了,再做下一次。
思路:这个要注意和显示生活中区别,这里是每天股票的价格已经知道了,所以,买卖的时候,只要是盈利的都做。大致过程是,对相邻两天的股票价格比较,只要后者大于前者,这次买卖就做。代码如下:
class Solution {
public:
int maxProfit(vector<int> &prices)
{
int profit=;
int len=prices.size();
for(int i=;i<len-;++i)
{
if(prices[i+]-prices[i]>)
profit+=prices[i+]-prices[i];
}
return profit;
}
};
这里值得注意的是:若代码中第6、7两行写成如下形式,是不能AC的。个人认为是prices.size()的类型为vector<int>::size_type,而减1以后,在prices.size()=0的情况,会负溢出。导致i小于一个正整数,但是数组中没有值,对数组进行访问时,会发生段问题。
(自己的考虑,没有考虑到类型限制,详情见下面)
for(int i=;i<prices.size()-;++i)
{
......
}
在本地验证的代码:
#include<iostream>
#include<vector> using namespace std; int main()
{
vector<int> a ;
cout << a.size() - << endl;
return ;
} //输出42949647295
解决的方法:一、之前加if判断,二、使用最上面代码中的那种方式--先求数组大小,然后用这个值去参与for循环。
//更新
这里给出Grandyang的回答:因为prices.size()返回的是size_type型的,这是一种unsigned类型,也就是无符号类型,所以在直接对其做减法是不行的,因为负数对其来说不合法,所以我们可以先强制将其转为整型,再做法,比如:
for(int i = ; i < (int)prices.size() - ; ++i)
{
......
}
(感谢Grandyang!!!)
[Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机的更多相关文章
- [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
- 122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票) ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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 IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 123 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III
假设你有一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易.注意:你不可同时参与多笔交易(你必须在再次购买前出售掉之前的股票).详见: ...
- 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV
假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
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 ...
随机推荐
- HTML基础实例
本节列举了一些简单的HTML例子,帮助大家更感性地认识HTML标签.是不是对一些标签还不熟悉?别担心,后面几个章节会有详细说明,先跑几个例子看看效果吧. HTML文档相关标签所有HTML文档必须以&l ...
- 安装docker和更改docker镜像下载目录
centos6.x系列: yum install http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm yum inst ...
- 46.VUE学习之--组件之使用动态组件灵活设置页面布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mysql 常用函数,基本使用
1:选中排除表1 连接表2 表3 获取选中表1中部分选中表3 的部分 并且设置选中状态select t1.*,if(t2中t3id=t1.id,1,0)as checked from t1 lefet ...
- 中恳中笨 搭建flask封装环境
话不多说,先干再说..... 打开pycharm,创建一个关于flask的项目 2.创建一个App的文件包 3.把staic和templates文件包拖进App里 4.把app.py文件改为manag ...
- go web处理上传
要使表单能够上传文件,第一步就是添加form的enctype属性,enctype属性有如下三种情况: application/x-www-form-urlencoded 表示在发送前编码所有字符(默认 ...
- golang 三个点的用法
已经忘了这是第几次查这个用法了,还是记一下吧~ ^ _ ^ 本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/137 ...
- 03---Nginx配置文件
#启动子进程程序默认用户#user nobody;#一个主进程和多个工作进程.工作进程是单进程的,且不需要特殊授权即可运行:这里定义的是工作进程数量worker_processes 1; #全局错误日 ...
- Python3爬虫(十) 数据存储之非关系型数据库MongoDB
Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.非关系型数据库NoSQL全程是Not Only SQL,非关系型数据库.NoSQL是基于键值对的,不需要经过S ...
- 触发显示和隐藏 div
<script> window.onload = function(){ var oDiv1 = document.getElementById("div1"); va ...