题解 | #买卖股票的最好时机#

买卖股票的最好时机

http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec

思路:最大利润=max(当前价格-历史最小价格)

import java.util.*; public class Solution {     /**      *       * @param prices int整型一维数组       * @return int整型      */     public int maxProfit (int[] prices) {         if(prices==null||prices.length<2){             return 0;         }         int minPrices = prices[0];         int maxProfit = 0;         for(int i=0; i<prices.length; i++){             if(prices[i]<minPrices){                 minPrices = prices[i];             }             int profit = prices[i]-minPrices;             if(profit>maxProfit){                 maxProfit=profit;             }         }         return maxProfit;     } }

全部评论

相关推荐

迷茫的大四🐶:你这个拿去投央国企吧,投私企包过不了的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务