Skip to content

Commit b16913b

Browse files
authored
feat(ml): $122.best-time-to-buy-and-sell-stock-ii.md (#414)
1 parent 9b565ce commit b16913b

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

problems/122.best-time-to-buy-and-sell-stock-ii.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Explanation: In this case, no transaction is done, i.e. max profit = 0.
5353

5454
## 代码
5555

56-
语言支持:JS,Python
56+
语言支持:JS,C++,Java,Python
5757

5858
JS Code:
5959

@@ -74,7 +74,42 @@ var maxProfit = function(prices) {
7474
return profit;
7575
};
7676
```
77+
C++ Code:
78+
79+
```c++
80+
class Solution {
81+
public:
82+
int maxProfit(vector<int>& prices) {
83+
int res = 0;
84+
for(int i=1;i<prices.size();i++)
85+
{
86+
if(prices[i] > prices[i-1])
87+
{
88+
res += prices[i] - prices[i-1];
89+
}
90+
}
91+
return res;
92+
}
93+
};
94+
```
7795
96+
Java Code:
97+
98+
```java
99+
class Solution {
100+
public int maxProfit(int[] prices) {
101+
int res = 0;
102+
for(int i=1;i<prices.length;i++)
103+
{
104+
if(prices[i] > prices[i-1])
105+
{
106+
res += prices[i] - prices[i-1];
107+
}
108+
}
109+
return res;
110+
}
111+
}
112+
```
78113

79114

80115
Python Code:

0 commit comments

Comments
 (0)