有一组数组代表股票的价格

一次买一次卖如何得到最大利润?

  public int maxProfit(int[] prices) {
if(prices.length==0)return 0;
int maxProfit=0;
int min=prices[0];
for(int i=0;i<prices.length;i++)
{
maxProfit=prices[i]-min>maxProfit?prices[i]-min:maxProfit;
min=prices[i]<min?prices[i]:min;
} return maxProfit; }

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

  1. leetcode 121. Best Time to Buy and Sell Stock ----- java

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

  2. Leetcode-Best Time to Buy and Sell Stock -java

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

  3. 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 ...

  4. leetcode:122. Best Time to Buy and Sell Stock II(java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...

  5. 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

    [121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...

  6. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

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

  7. [LeetCode][Java] 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 a ...

  8. [LeetCOde][Java] Best Time to Buy and Sell Stock III

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

  9. [LeetCode] 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 ...

随机推荐

  1. JS操作URL

    function getQueStr(url, ref) //取获参数值 { ); ) { var arr = str.split('&'); for (i in arr) { ] == re ...

  2. IOS UI篇—UILabel的文字顶部对齐

    UILabel的文字顶部对齐 NOV 20TH, 2011 默认UILabel是垂直居中对齐的,如果你的UILabel高度有多行,当内容少的时候,会自动垂直居中. 如下图所示(图片来自stackove ...

  3. J2EE项目开发流程简介

    开发流程(一) 提出需求:产品部提出本周期项目的具体需求. 项目计划:项目经理协调开发部.测试部和产品部进行需求协商,产生项目计划. 需求理解:开发部和测试部向产品部提出各自对需求的理解. 产品设计: ...

  4. ARM编译空间属性(转)

    原文地址:http://www.cnblogs.com/hongzg1982/articles/2205093.html 1. 程序的空间属性 一般情况下,一个程序本质上都是由 bss段.data段. ...

  5. JAVA不经过Catch(Exception e)直接到finally或者退出原因

    今天遇到一个很奇葩的问题!在写Hadoop程序的时候!new一个对象!程序直接跑到finally代码块里面去了!Catch里面的Exception也没有执行. Configuration config ...

  6. php 数组Array 删除指定键名值

    if(array_key_exists('keyname',$array)){ //检查数组中此键名是否存在: unset($array['keyname']); //删除后位置仍然保留,但清空了键名 ...

  7. LaTeX使用titlesec宏包改变章节编号形式的方法

    1.titleformat宏包命令详解 LaTeX中可以用titlesec宏包中的titleformat命令来改变标题形式: 导入宏包: \usepackage{titlesec} 改变标题的代码如下 ...

  8. requirejs学习之-- 初始化(一)

    为了规范在项目中使用的javascript代码,我们使用了requirejs框架. 初始阶段,我们在按钮的点击事件中调用创建的模块,代码如下: function button_click() { _t ...

  9. FileAttributes枚举

    FileAttributes枚举是一个专门用于标记硬盘上的文件属性的枚举,枚举的说明在这里:http://www.cnblogs.com/kissdodog/archive/2013/01/16/28 ...

  10. LeeCode-Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has For exampl ...