更新 Solution.java

master
wangsiyuan 2023-09-13 11:49:23 +08:00
parent cd41388601
commit 95ea49c5a0
1 changed files with 20 additions and 0 deletions

View File

@ -184,4 +184,24 @@ public class Solution {
}
return i;
}
/**
* nums
* true false
* @param nums
* @return
*/
public static boolean canJump(int[] nums) {
int maxReach = 0; // 初始化最远可到达的位置为0
for (int i = 0; i < nums.length; i++) {
if (i > maxReach) {
// 如果当前索引超过了最远可到达的位置,说明无法到达终点
return false;
}
// 更新最远可到达的位置
maxReach = Math.max(maxReach, i + nums[i]);
}
return true;
}
}