Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fa6e237

Browse files
authored
添加 0714.买卖股票的最佳时机含手续费 go版本
添加 0714.买卖股票的最佳时机含手续费 go版本
1 parent 7225173 commit fa6e237

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

problems/0714.买卖股票的最佳时机含手续费.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,30 @@ class Solution: # 贪心思路
216216
```
217217

218218
Go:
219-
219+
```golang
220+
func maxProfit(prices []int, fee int) int {
221+
var minBuy int = prices[0] //第一天买入
222+
var res int
223+
for i:=0;i<len(prices);i++{
224+
//如果当前价格小于最低价,则在此处买入
225+
if prices[i]<minBuy{
226+
minBuy=prices[i]
227+
}
228+
//如果以当前价格卖出亏本,则不卖,继续找下一个可卖点
229+
if prices[i]>=minBuy&&prices[i]-fee-minBuy<=0{
230+
continue
231+
}
232+
//可以售卖了
233+
if prices[i]>minBuy+fee{
234+
//累加每天的收益
235+
res+=prices[i]-minBuy-fee
236+
//更新最小值(如果还在收获利润的区间里,表示并不是真正的卖出,而计算利润每次都要减去手续费,所以要让minBuy = prices[i] - fee;,这样在明天收获利润的时候,才不会多减一次手续费!)
237+
minBuy=prices[i]-fee
238+
}
239+
}
240+
return res
241+
}
242+
```
220243
Javascript:
221244
```Javascript
222245
// 贪心思路

0 commit comments

Comments
 (0)