Algorithms—122.Best Time to Buy and Sell Stock II

来源:互联网 发布:简历管理系统 源码 编辑:程序博客网 时间:2024/06/05 15:10

思路:比121题更简单,不限制交易次数。

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


0 0
原创粉丝点击