Best Time to Buy and Sell Stock (java)
有一组数组代表股票的价格
一次买一次卖如何得到最大利润?
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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
[121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- Global.asax使用2
ASP.NET中利用Application和Session统计在线人数.历史访问量 先来简单说一下ASP.NET中的Application和Session 下图是我们非常熟悉的Web应用程序的结构: ...
- 如何安装,卸载eclipse中的ADT,或者更新ADT
昨天手贱把Android的SDK升级了,然后搞到ADT的版本不对应,然后从网上搜了一些办法~效果还可以,重新安装了一次ADT. 卸载ADT的方法,方法如下(我的Eclipse版本为3.5): 1.选择 ...
- (转载)Windows下手动完全卸载Oracle
使用Oracle自带的Universal Installer卸载存在问题: 不干净,不完全,还有一些注册表残留,会影响到后来的安装. 所以,推荐使用手工卸载Oracle. 1.[win+R]-> ...
- iOS-设计模式之通知
通知设计模式简单好用,就是一个项目中如果用的太多,不利于代码维护,可读性太差. 实现过程: [[NSNotificationCenter defaultCenter]postNotificationN ...
- 一、Autofac入门
想要将autofac集成到你的应用程序中需要经过如下步骤: 1.使用控制翻转(IoC)的思想架构你的应用程序: 2.添加autofac引用: 3.在应用程序入口...(At application s ...
- JS截取字符串:slice(),substring()和substr()
var string='abcdefg' 1.slice() string.slice(startLocation [, endLocation]) ps1:2个参数可以为负数,若参数值为负数,则将该 ...
- zeromq源码分析笔记之准备(0)
zeromq这个库主要用于进程通信,包括本地进程.网络通信,涉及到一些基础知识,主要包括管道通信,socket编程的内容,反应器模式(使用IO多路复用实现),无锁队列这几块比较重要的部分,下面的几个链 ...
- Mr. Kitayuta's Colorful Graph 多维并查集
Mr. Kitayuta's Colorful Graph 并查集不仅可以用于一维,也可以用于高维. 此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的 ...
- 使用URLConnection获取网页信息的基本流程
参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...
- jq里attr和prop的区别
jquery中attr和prop的区别 在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答 ...