zoukankan      html  css  js  c++  java
  • leetcode122:买卖股票的最佳时机Ⅱ

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

    设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

    注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

    ====================Python===================

    class Solution:
        def maxProfit(self, prices: List[int]) -> int:
            max_profit = 0
            for i in range(1, len(prices)):
                if prices[i] > prices[i-1]:
                    max_profit += prices[i] - prices[i-1]
            return max_profit

    =====================Java=====================

    class Solution {
        public int maxProfit(int[] prices) {
            int res = 0;
            for (int i = 1; i < prices.length; i++) {
                if (prices[i] > prices[i-1]) {
                    res += prices[i] - prices[i-1];
                }
            }
            return res;
        }
    }

    ===================Go============================

    func maxProfit(prices []int) int {
        res := 0
        for i := 1; i < len(prices); i++{
            if prices[i] > prices[i-1] {
                res += prices[i] - prices[i-1]
            }
        }
        return res
    }
  • 相关阅读:
    css半透明边框
    css脱离文档流
    margin负值的作用
    浅谈BFC
    css多列布局
    css布局--水平垂直居中
    css布局--垂直居中
    css布局--水平居中
    题解-APIO2019奇怪装置
    题解-AtCoder ARC-078F Mole and Abandoned Mine
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13493009.html
Copyright © 2011-2022 走看看