添加 0122.买股票的最佳时机II.md C语言实现

This commit is contained in:
ArthurP
2021-08-28 20:51:26 +08:00
parent 3107ba208c
commit d7cadd601f

View File

@ -229,6 +229,21 @@ var maxProfit = function(prices) {
};
```
C:
```c
int maxProfit(int* prices, int pricesSize){
int result = 0;
int i;
//从第二个元素开始遍历数组,与之前的元素进行比较
for(i = 1; i < pricesSize; ++i) {
//若该元素比前面元素大,则说明有利润。代表买入
if(prices[i] > prices[i-1])
result+= prices[i]-prices[i-1];
}
return result;
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)