Skip to content

Commit d7cadd6

Browse files
添加 0122.买股票的最佳时机II.md C语言实现
1 parent 3107ba2 commit d7cadd6

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

problems/0122.买卖股票的最佳时机II.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,21 @@ var maxProfit = function(prices) {
229229
};
230230
```
231231

232+
C:
233+
```c
234+
int maxProfit(int* prices, int pricesSize){
235+
int result = 0;
236+
int i;
237+
//从第二个元素开始遍历数组,与之前的元素进行比较
238+
for(i = 1; i < pricesSize; ++i) {
239+
//若该元素比前面元素大,则说明有利润。代表买入
240+
if(prices[i] > prices[i-1])
241+
result+= prices[i]-prices[i-1];
242+
}
243+
return result;
244+
}
245+
```
246+
232247
-----------------------
233248
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
234249
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

0 commit comments

Comments
 (0)