原有思路是用c++实现的,不过主要思路是一致的,首先当数组的长度为0或者1以下的时候,我们所买的股票价格为不买股票且没赚头:所有return 0
在第二步使用,所有只要拥有后面值比前面值大的数值都将他们的差累加起来进行最终累加。
class Solution {
public int maxProfit(int[] prices) {
int size = prices.length;
if (size <= 1 ){
return 0;
}
else{
int max = 0;
for (int i = 1; i < size; i++){
if(prices[i]>prices[i-1]){
max += (prices[i]-prices[i-1]);
}
}
return max;
}
}
}