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

买卖股票的最好时机

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

import java.util.*;


public class Solution {
    /**
     * 
     * @param prices int整型一维数组 
     * @return int整型
     */
    public int maxProfit (int[] prices) {
        if (prices.length <= 1) {
            return 0;
        }
        int maxProfit = 0;
        int lowPrice = 10001;
        for (int i = 0; i < prices.length; i++) {
            if (lowPrice > prices[i]) {
                lowPrice = prices[i];
            }
            if (prices[i] - lowPrice > maxProfit) {
                maxProfit = prices[i] - lowPrice;
            }
        }
        return maxProfit;
    }
}
全部评论

相关推荐

酷酷我灵儿帅:这去不去和线不线下面说实话没啥关系
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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