漫画:程序教你玩转股票





































public class StockProfit {public static int maxProfitFor1Time(int prices[]) {if(prices==null || prices.length==0) {return 0;}int minPrice = prices[0];int maxProfit = 0;for (int i = 1; i < prices.length; i++) {if (prices[i] < minPrice) {minPrice = prices[i];} else if(prices[i] - minPrice > maxProfit){maxProfit = prices[i] - minPrice;}}return maxProfit;}public static void main(String[] args) {int[] prices = {9,2,7,4,3,1,8,4};System.out.println(maxProfitFor1Time(prices));}}







public int maxProfitForAnyTime(int[] prices) {int maxProfit = 0;for (int i = 1; i < prices.length; i++) {if (prices[i] > prices[i-1])maxProfit += prices[i] - prices[i-1];}return maxProfit;}







更多精彩推荐
点分享 点点赞 点在看
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/
关注网络尖刀微信公众号随时掌握互联网精彩
赞助链接
排名
热点
搜索指数
- 1 应变克难开新局 7904392
- 2 日舰曾收到中方提示 7807874
- 3 日本气象厅:一周内或发生9级地震 7713376
- 4 “好房子”长啥样 7616367
- 5 你点的三家外卖可能出自同一口锅 7522107
- 6 中方回应向日本出口稀土出现延误 7427912
- 7 受贿超11亿!白天辉被执行死刑 7328145
- 8 一定要在这个年龄前就开始控糖 7236645
- 9 音频实证日本有意滋扰中方训练 7140469
- 10 入冬以来最大范围风雪天气来了 7043324










程序人生
