LeetCode--122:买卖股票的最佳时机(java)

原题链接
思路很简单,直接上代码

	public int maxProfit(int[] prices) {
        int maxprofit = 0;
        //遍历数组,只要当天的价格相对于昨天是上涨的,那就卖出,计算利润
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1])
                maxprofit += prices[i] - prices[i - 1];
        }
        return maxprofit;
    }
发布了24 篇原创文章 · 获赞 3 · 访问量 555

猜你喜欢

转载自blog.csdn.net/QinLaoDeMaChu/article/details/103943425