Skip to content

Commit fc21467

Browse files
committed
feat: add question 55
1 parent 277cbec commit fc21467

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

55.跳跃游戏.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode.cn id=55 lang=javascript
3+
*
4+
* [55] 跳跃游戏
5+
*
6+
* 1. 从后向前遍历, 记录最后一个可达位置
7+
* 2. 如果 当前位置 + 可跳跃距离 >= 最后一个可达位置, 则说明当前位置可达, 更新可达位置
8+
* 3. 结果取决于 位置0 的可达情况
9+
*/
10+
11+
// @lc code=start
12+
/**
13+
* @param {number[]} nums
14+
* @return {boolean}
15+
*/
16+
var canJump = function(nums) {
17+
let last = nums.length - 1;
18+
for (let i = nums.length - 2; i >= 0; i--) {
19+
if (i + nums[i] >= last) {
20+
last = i;
21+
}
22+
if (i === 0) {
23+
return i === last;
24+
}
25+
}
26+
27+
return true;
28+
};
29+
// @lc code=end

0 commit comments

Comments
 (0)