这道题想了很多,但是想多了。这个题思路很简单,如果当前值大于最小值,就计算差,和最大利润值比较。

 class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size()==)
return ;
int length = prices.size();
int minVal = prices[];
int maxP = ; for(int i=;i<length;i++)
{
if(prices[i]<minVal)
minVal = prices[i];
if(prices[i]-minVal>maxP)
maxP = prices[i]-minVal;
}
return maxP;
}
};

Best Time to Sell and Buy Stock的更多相关文章

  1. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. Best Time to Buy and Sell Stock with Cooldown

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. LeetCode Best Time to Buy and Sell Stock with Cooldown

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目: Say you hav ...

  4. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  5. Best Time to Buy and Sell Stock | & || & III

    Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...

  6. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票

    121. Say you have an array for which the ith element is the price of a given stock on day i. If you ...

  8. 【LeetCode OJ】Best Time to Buy and Sell Stock II

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...

  9. 309. Best Time to Buy and Sell Stock with Cooldown

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

随机推荐

  1. 在银行业中,BP是指什么?

    基点 Basis Point(BP)债券和票据利率改变量的度量单位.一个基点等于0.01个百分点,即0.01%,因此,100个基点等于1%.[例]一浮动利率债券的利率可能比LIBOR高10个基点,10 ...

  2. 3.appium定位方法

    1.使用id定位: driver.find_element_by_id('id的名称').click() 2.使用className定位: driver.find_element_by_class_n ...

  3. c++设计模式:单例模式

    1.设计思想: 单例模式,顾名思义,即一个类只有一个实例对象.C++一般的方法是将构造函数.拷贝构造函数以及赋值操作符函数声明为private级别,从而阻止用户实例化一个类.那么,如何才能获得该类的对 ...

  4. import schedule ImportError: No module named schedule

    安装pip sudo apt-get install python-pip 安装schedule模块 pip install schedule PS: 如果已经安装pip,可能出现以下问题,按照提示重 ...

  5. 前端(Node.js)(2)-- Node.js开发环境配置

    1.开发环境介绍 1.MEAN Stack 什么是全栈? 负责界面和UI的设计师.负责移动端应用开发的安卓IOS开发工程师.负责服务器端开发的后端程序员.负责数据库开发和管理的数据库工程师.负责服务器 ...

  6. web前端学习(四)JavaScript学习笔记部分(6)-- js内置对象

    1.JS内置对象-什么是对象 1.1.什么是对象: JavaScript中的所有事物都是对象:字符串.数值.数组.函数 每个对象带有属性和方法 JavaScript允许自定义对象 1.2.自定义对象: ...

  7. 直接在安装了redis的Linux机器上操作redis数据存储类型--hash类型

    一.概述:   我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息.如Username.Password和 ...

  8. python Pandas文件读写

  9. MaxCompute Spark开发指南

    0. 概述 本文档面向需要使用MaxCompute Spark进行开发的用户使用.本指南主要适用于具备有Spark开发经验的开发人员. MaxCompute Spark是MaxCompute提供的兼容 ...

  10. 【python之路14】发送邮件实例

    1.发邮件的代码 from email.mime.text import MIMEText from email.utils import formataddr import smtplib msg ...