Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a031937

Browse files
committed
添加 0714.买卖股票的最佳时机含手续费.md Scala版本
1 parent 919da74 commit a031937

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public:
153153

154154
## 其他语言版本
155155

156-
Java:
156+
### Java
157157
```java
158158
// 贪心思路
159159
class Solution {
@@ -198,7 +198,7 @@ class Solution { // 动态规划
198198

199199

200200

201-
Python:
201+
### Python
202202

203203
```python
204204
class Solution: # 贪心思路
@@ -216,7 +216,7 @@ class Solution: # 贪心思路
216216
return result
217217
```
218218

219-
Go:
219+
### Go
220220
```golang
221221
func maxProfit(prices []int, fee int) int {
222222
var minBuy int = prices[0] //第一天买入
@@ -241,7 +241,7 @@ func maxProfit(prices []int, fee int) int {
241241
return res
242242
}
243243
```
244-
Javascript:
244+
### Javascript
245245
```Javascript
246246
// 贪心思路
247247
var maxProfit = function(prices, fee) {
@@ -293,7 +293,7 @@ var maxProfit = function(prices, fee) {
293293
};
294294
```
295295

296-
TypeScript:
296+
### TypeScript
297297

298298
> 贪心
299299

@@ -335,8 +335,28 @@ function maxProfit(prices: number[], fee: number): number {
335335
};
336336
```
337337

338-
339-
338+
### Scala
339+
340+
贪心思路:
341+
342+
```scala
343+
object Solution {
344+
def maxProfit(prices: Array[Int], fee: Int): Int = {
345+
var result = 0
346+
var minPrice = prices(0)
347+
for (i <- 1 until prices.length) {
348+
if (prices(i) < minPrice) {
349+
minPrice = prices(i) // 比当前最小值还小
350+
}
351+
if (prices(i) > minPrice + fee) {
352+
result += prices(i) - minPrice - fee
353+
minPrice = prices(i) - fee
354+
}
355+
}
356+
result
357+
}
358+
}
359+
```
340360

341361
-----------------------
342362
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /