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 74a422c

Browse files
committed
添加 0045.跳跃游戏II.md Scala版本
1 parent b0664fc commit 74a422c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

‎problems/0045.跳跃游戏II.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,31 @@ function jump(nums: number[]): number {
279279
};
280280
```
281281

282-
282+
### Scala
283+
284+
```scala
285+
object Solution {
286+
def jump(nums: Array[Int]): Int = {
287+
if (nums.length == 0) return 0
288+
var result = 0 // 记录走的最大步数
289+
var curDistance = 0 // 当前覆盖最远距离下标
290+
var nextDistance = 0 // 下一步覆盖最远距离下标
291+
for (i <- nums.indices) {
292+
nextDistance = math.max(nums(i) + i, nextDistance) // 更新下一步覆盖最远距离下标
293+
if (i == curDistance) {
294+
if (curDistance != nums.length - 1) {
295+
result += 1
296+
curDistance = nextDistance
297+
if (nextDistance >= nums.length - 1) return result
298+
} else {
299+
return result
300+
}
301+
}
302+
}
303+
result
304+
}
305+
}
306+
```
283307

284308

285309

0 commit comments

Comments
(0)

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