目录标题
- 2023-8-10 16:27:05
55. 跳跃游戏
2023-8-10 16:27:05
class Solution {public boolean canJump(int[] nums) {int n = nums.length;int arrivalLocation = 0;for (int i = 0; i < n; ++i) {if (i <= arrivalLocation) {arrivalLocation = Math.max(arrivalLocation, i + nums[i]);if (arrivalLocation >= n - 1) {return true;}}}return false;}
}
贪婪算法思路:每一个点我能跳跃的情况,全部都跳跃一次(每一个点的最优解),如果能够跳跃出长度或者到达了最后点,那么我就是肯定可达最终点的;否则就是不可达的。(局部最优解就能够得出整体的最优解)