-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path55.跳跃游戏.cpp
More file actions
68 lines (66 loc) · 1.46 KB
/
55.跳跃游戏.cpp
File metadata and controls
68 lines (66 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* @lc app=leetcode.cn id=55 lang=cpp
*
* [55] 跳跃游戏
*
* https://leetcode-cn.com/problems/jump-game/description/
*
* algorithms
* Medium (43.47%)
* Likes: 1615
* Dislikes: 0
* Total Accepted: 396.2K
* Total Submissions: 911.6K
* Testcase Example: '[2,3,1,1,4]'
*
* 给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
*
* 数组中的每个元素代表你在该位置可以跳跃的最大长度。
*
* 判断你是否能够到达最后一个下标。
*
*
*
* 示例 1:
*
*
* 输入:nums = [2,3,1,1,4]
* 输出:true
* 解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
*
*
* 示例 2:
*
*
* 输入:nums = [3,2,1,0,4]
* 输出:false
* 解释:无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。
*
*
*
*
* 提示:
*
*
* 1
* 0
*
*
*/
// @lc code=start
class Solution {
public:
bool canJump(vector<int>& nums) {
// 从起点可以到达第i点时,则必然可以到达第i-1点
for(int i=0,j=0;i<nums.size();++i) {
// 可跳到的点j<当前点i
if(j<i) {
return false;
}
// 可以跳跃到点i,重新计算可以跳到的最远格子
j=max(j,i+nums[i]);
}
return true;
}
};
// @lc code=end