mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
添加 0122.买股票的最佳时机II.md C语言实现
This commit is contained in:
@ -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)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
Reference in New Issue
Block a user