题解 | #买卖股票#

买卖股票的最好时机(一)

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

import java.util.*;

//首先确定历史最低价格 //根据当前价格和历史最低价格算出利润,求出最大利润 //重复以上过程知道遍历完数组为止 //买卖股票怎么能这么简单?不要想太多 public class Solution { /** * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { int min = prices[0]; int max = 0; int size = prices.length;

    for( int i = 0;i  < size;i ++){
        min = Math.min(min,prices[i]);
        max = Math.max(max,prices[i] - min);
           
    }
    return max;
    // write code here
}

}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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