题目:
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
class Solution {
public:
bool canJump(int A[], int n) {
int jump = A[0];
for(int i = 1; i < n; i++) {
if(jump <= 0)
return false;
else {
--jump;
jump = max(jump, A[i]);
}
}
return true;
}
};
本文探讨了一个基于数组实现的跳远游戏逻辑,玩家通过数组元素确定每一步的最大跳跃距离,判断是否能够到达游戏终点。通过实例分析,展示了如何通过简单的编程技巧解决跳跃路径问题。
369

被折叠的 条评论
为什么被折叠?



