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

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

  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. Common Table Expressions (CTE)

    子查询有时使用起来很麻烦,因为所有的过滤和匹配逻辑都必须集成到子查询表达式中.如果只需要执行一个任务,且只需要使用一次杳询表达式,子查询是很好的选择.但子查询不能被重用,也不能很好地支持多个需求.这个 ...

  2. Java Timer触发定时器

    XML: <!-- Java Timer定时 --> <!-- <bean id="shortUrlTask" class=" com.sprin ...

  3. 真机调试以及“Could not find Developer Disk Image”问题解决方案

    真机测试步骤 1.运行Xcode,Xcode打开后,点左上角菜单'Xcode',点'Preferences'. 2.在打开的窗口中,点'Accounts',切换到账号页,然后点下面的'+'号,在弹出菜 ...

  4. javascript父窗口与子窗口通信

    在父窗口的js代码 //回调的函数 function alt(temp){ alert(temp); }; $("#Btn").click(function (obj) { //声 ...

  5. 我的第一个QML Button的实现

    编写第一个QML,在成功跑完HelloWorld后,决定自己实现Button按钮类. Button是在Quick2版本以上的QtQuick Controls出现的. 在Qt5.5.1版本中,选择插入Q ...

  6. 配置sphinx

    1.先安装sphinxclient    #cd /usr/local/src    #wget http://sphinxsearch.com/files/sphinx-0.9.9.tar.gz   ...

  7. sql server中主键列的插入问题

    仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'dbo.t_test'中的标识列指定显式值. SET IDENTITY_INSERT dbo.t_test ON ,'c' ...

  8. python保留指定文件、删除目录其他文件的功能(2)

    在(1)中脚本实现了保留指定文件的功能,但不能删除空目录,在此补上删除空目录的方法 def DeleteEmptyDir(path): for i in range(1,100): for paren ...

  9. Loadrunner11 录制手机App脚本多种方法介绍

    总体来说,通过LR录制手机脚本的方式有三种:1)通过代理方式录制,保证手机电脑在同一个网段:2)通过抓包录制,在手机上安装Mobile Recorder:3)通过安卓模拟器录制,本地安装Android ...

  10. MongoDB insert performance rapidly dropping

    http://dba.stackexchange.com/questions/65554/mongodb-insert-performance-rapidly-dropping http://www. ...