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

 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. 【MFC学习笔记】菜单和工具栏

    1 菜单栏 1.1 在对话框中加入菜单: 打开Resource View资源视图, *.rc文件—Add Resources —Menu,即可加入菜单. 注:①菜单项中含有“...”表示点击后会弹出对 ...

  2. 关于解码芯片SAA7115及7105的配置 ,FVID与微驱动

    最近因为在做视频处理,所以和解码芯片打交道很多,根据DM642的例程总了一些应该配置的东西. 注:以下的配置均是指AV信号输入,PAL制式输出的条件 (一)关于SAA7115(基于TI的驱动) 结构体 ...

  3. NPM:如何配置maven npm私服

    https://help.sonatype.com/repomanager3/quick-start-guide-proxying-maven-and-npm#QuickStartGuide-Prox ...

  4. Java数据结构和算法(八)--红黑树与2-3树

    红黑树规则: 1.每个节点要么是红色,要么是黑色 2.根节点都是黑色节点 3.每个叶节点是黑色节点 3.每个红色节点的两个子节点都是黑色节点,反之,不做要求,换句话说就是不能有连续两个红色节点 4.从 ...

  5. [Array] 561. Array Partition I

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  6. Win7+AMD+VS2013+opencl1.x安装与测试

    参考资料:http://www.cnblogs.com/lihao602/archive/2013/05/08/3067239.html: http://blog.csdn.net/zhoubo616 ...

  7. JQuery--extend的使用

    $.extend({a,b,c},{a,bd}); 里面的对象属性如果 存在就替换,如果不存在就添加 var loadData = function (order) { var obj = getPa ...

  8. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  9. 锋利的JQuery学习之JQuery中的事件

    一.加载DOM 在页面加载完毕之后,浏览器会通过javascript为dom元素添加事件,在常规的javascript中使用window.onload方法,而在jQuery中使用的是$(documen ...

  10. FFT初步代码分析和逼近曲线

    FFT:快速傅里叶变换 文章从两个方面来写,一个是FFT的基础知识,也就是将时域信号转换为频域信号,另一个是合成时域信号. 将时域信号转换为频域信号 代码来源于http://bigsec.net/b5 ...