leetcode:121. Best Time to Buy and Sell Stock(Java)解答

原创 2016年03月05日 17:14:05

转载请注明出处:z_zhaojun的博客
原文地址
题目地址
Best Time to Buy and Sell Stock My Submissions Question

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

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

解法(java):数组中第i个数字减去前面i-1个数字中的最小数字即为从前i个股票价格变化中可得到的最大收益。

public int maxProfit(int[] prices) {
        int maxPro = 0;
        int minPrice = Integer.MAX_VALUE;
        for (int price : prices) {
            maxPro = Math.max(maxPro, price - minPrice);
            minPrice = Math.min(minPrice, price);
        }
        return maxPro;
    }
版权声明:本文为博主原创文章,转载请注明出处。博客首页:http://blog.csdn.net/u012975705。订阅:http://blog.csdn.net/u012975705/rss/list

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

【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Say you have a...

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 we...
  • NOadu
  • NOadu
  • 2017年07月08日 15:16
  • 114

【小熊刷题】Best Time to Buy and Sell Stock I <leetcode 121 Java>

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

Leetcode 121. Best Time to Buy and Sell Stock

噢?一脸黑人问号,这道题目怎么会比之前那题通过率更低?刷完才发现,好像解题方式完全不同,题目的含义大相径庭。题目Say you have an array for which the ith elem...

LeetCode121—Best Time to Buy and Sell Stock

LeetCode121—Best Time to Buy and Sell Stock

Leetcode 121 Best Time to Buy and Sell Stock 单次买卖股票最大收益

假设你有一个数组,里面记录的是每一天的股票的价格。 如果你至多只能完成一次交易(即,买一次股票、卖一次股票),设计一个算法来计算最大收益。...

leetcode_[python/C++]_121/122/123/188.Best Time to Buy and Sell Stock I/II/III/IV

121. Best Time to Buy and Sell Stock [题目] Say you have an array for which the ith element is the p...

LeetCode-121&122&123&188.Best Time to Buy and Sell Stock

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Say you have an array for which the...

[Leetcode刷题总结系列][Dynamic programming] 121. Best time to buy and sell stock

Leetcode 121. Best time to buy and sell stock

[Leetcode][Dynamic programming] 121. Best time to buy and sell stock

Leetcode 121. Best time to buy and sell stock. Dynamic Programming
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:121. Best Time to Buy and Sell Stock(Java)解答
举报原因:
原因补充:

(最多只允许输入30个字)