z_zhaojun的博客

机会只会留给有准备的人

leetcode:122. Best Time to Buy and Sell Stock II(java)解答

转载请注明出处:z_zhaojun的博客
原文地址
题目地址
Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

解答(Java):

public int maxProfit(int[] prices) {
        int maxPro = 0;
        int length = prices.length - 1;
        for (int i = 0; i < length; i++) {
            if (prices[i] < prices[i + 1]) {
                maxPro += prices[i + 1] - prices[i];
            }
        }
        return maxPro;
    }
阅读更多
版权声明:本文为博主原创文章,转载请注明出处。博客首页:http://blog.csdn.net/u012975705。订阅:http://blog.csdn.net/u012975705/rss/list https://blog.csdn.net/u012975705/article/details/50809894
个人分类: LeetCode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭