zoukankan      html  css  js  c++  java
  • 121. 买卖股票的最佳时机

    package cn.jiedada;

    import org.junit.Test;

    import java.util.*;

    /**给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,
    * 判断字符串是否有效。有效字符串需满足:左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。
    */
    class Solution {
    public static void main(String[] args) {
    String s ="()";
    }

    /**
    * 我们需要找到最小值,当出现最小值的时候,我们的最大值只能为后面的数
    * @param prices
    * @return
    */
    public int maxProfit(int[] prices) {
    int minPrice=prices[0],maxPrice=prices[0],profit=0;
    int temp,oneProfit=0;
    for (int price : prices) {
    temp=minPrice;
    //因为如果后面一来不是最小值2 6 1 3没有问题后面有一个利润最大值这种情况的话、
    // 如果1 3 2 6那一来就是最小值那肯定是后面的
    minPrice=Math.min(minPrice,price);
    if (temp==minPrice){
    //只有
    maxPrice = Math.max(maxPrice,price);
    oneProfit = maxPrice-minPrice;
    }else {
    //如果出现最小值的时候只能为后面的数
    maxPrice=minPrice;
    }
    //因为会出现多个时候的利润所以需要取最大值
    profit=Math.max(profit,oneProfit);
    }
    return profit;
    }
    }
  • 相关阅读:
    C#:将空间数据加载到树视图控件
    C# 常见错误
    C#:Application操作(待补充)
    C#:XML操作(简单)
    C#:xml操作(待补充)
    C#:消息框
    C#:数学运算(待补充)
    C#:Ini文件操作(待补充)
    C#:文件操作(待补充)
    2015河南省农村拆迁赔偿流程
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/15092658.html
Copyright © 2011-2022 走看看