LeetCode题解:Best Time to Buy and Sell Stock
2015-10-02 21:06
253 查看
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.
题意:找出买卖股票的最佳时机,只能完成一单交易
解决思路:……
代码:
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.
题意:找出买卖股票的最佳时机,只能完成一单交易
解决思路:……
代码:
public class Solution { public int maxProfit(int[] prices) { int maxCur = 0, maxSoFar = 0; for(int i = 1; i < prices.length; i++) { maxCur = Math.max(0, maxCur += prices[i] - prices[i-1]); maxSoFar = Math.max(maxCur, maxSoFar); } return maxSoFar; } }
相关文章推荐
- TCP/IP详解卷1 读书笔记:第七章 Ping程序
- Java学习笔记----封装和private关键字
- Android五个布局
- http://jingyan.baidu.com/article/a378c960630e61b329283045.html
- [codevs2152]滑雪
- 新文章
- LeetCode题解:Triangle
- [LeetCode]Longest Consecutive Sequence
- RPC漏洞
- CentOS 7更改网卡名称
- equal和==的区别(原理讲解)
- 大数乘法(快速傅立叶变换)下
- 三色旗
- Mysql命令大全
- http://www.linuxidc.com/Linux/2007-09/7399.htm
- LeetCode题解:Populating Next Right Pointers in Each Node
- Eclipse 项目导入 Android Studio 导致的乱码问题
- 第14章 探索虚拟内存(1)
- LeetCode题解:Flatten Binary Tree to Linked List
- Unity 下简易状态机的实现