题解 | #买卖股票的最好时机 ii#
买卖股票的最好时机 ii
https://www.nowcoder.com/practice/572903b1edbd4a33b2716f7649b4ffd4
import java.util.*; public class Solution { /** * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { int res = 0; for (int i = 0; i < prices.length - 1; i++) { int length = 1; while ((i + length) < prices.length && prices[i + length] > prices[i + length - 1]) { length++; } length--; res += prices[i + length] - prices[i]; i += length; } return res; } }