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;
    }
    }
  • 相关阅读:
    Leetcode 121. Best Time to Buy and Sell Stock
    Leetcode 120. Triangle
    Leetcode 26. Remove Duplicates from Sorted Array
    Leetcode 767. Reorganize String
    Leetcode 6. ZigZag Conversion
    KMP HDU 1686 Oulipo
    多重背包 HDU 2844 Coins
    Line belt 三分嵌套
    三分板子 zoj 3203
    二分板子 poj 3122 pie
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/15092658.html
Copyright © 2011-2022 走看看