# 55. Jump Game

You are given an integer array `nums`. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.

Return `true` if you can reach the last index, or`false` otherwise.

Example 1:

``````Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
``````

Example 2:

``````Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
``````

Constraints:

• `1 <= nums.length <= 10^4`
• `0 <= nums[i] <= 10^5`

``````class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size(), reach = 0;
for (int i = 0; i < n; ++i) {
if (i > reach || reach >= n - 1) break;
reach = max(reach, i + nums[i]);
}
return reach >= n - 1;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/55

Jump Game II

Jump Game III

Jump Game IV

Jump Game V

Jump Game VI

Jump Game VII

Jump Game VIII

Jump Game IX

Minimum Number of Visited Cells in a Grid

Largest Element in an Array after Merge Operations

https://leetcode.com/problems/jump-game/

https://leetcode.com/problems/jump-game/discuss/20917/Linear-and-simple-solution-in-C++

https://leetcode.com/problems/jump-game/discuss/20923/Java-Solution-easy-to-understand

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation