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

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

  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. Android5.0+(CollapsingToolbarLayout)

    CollapsingToolbarLayout作用是提供了一个可以折叠的Toolbar,它继承至FrameLayout,给它设置layout_scrollFlags,它可以控制包含在Collapsin ...

  2. ORA-01152错误解决方法(转)

    具体步骤如下: startup force; alter system set "_allow_resetlogs_corruption"=true scope=spfile; r ...

  3. CoutDownLatch 多线程同步辅助类

    CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); pu ...

  4. 关于iOS9之后的loadViewIfNeeded

    iOS9之前 有些时候因为需要手动调用loadview 但是有风险,系统不再调用viewDidLoad 所以手动调用loadview是错误的 iOS9之后出现了loadViewIfNeeded解决了这 ...

  5. 转帖:深入理解JavaScript系列

    感觉汤姆大叔这个系列写的很是不错,很适合有js基础但是想深入又无从下手的朋友. 深入理解JavaScript系列

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

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

  7. 关于UIWebview的属性的介绍

    /*    ViewController.h 文件               */ #import <UIKit/UIKit.h> @interface ViewController : ...

  8. Intel Core i7的整体操作

    Intel Core i7的整体操作(我们也称呼为Nehalem,他的项目代码名) 主要分成2个部分-指令控制单元Instruction Control Unit(ICU),负责从存储器读出指令序列, ...

  9. CentOS安装配置ganglia

    1.     下载ganglia源码包并解压 wget http://sourceforge.net/projects/ganglia/files/ganglia%20monitoring%20cor ...

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

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