diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 29aa5b83..bd837eea 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -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)