GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — [LeetCode] 55. Jump Game · Issue #55 · grandyang/leetcode Open Description
You are given an integer array Return Example 1: Example 2: Constraints:
这道题说的是有一个非负整数的数组,每个数字表示在当前位置的最大跳力(这里的跳力指的是在当前位置为基础上能到达的最远位置),求判断能不能到达最后一个位置,开始博主以为是必须刚好到达最后一个位置,超过了不算,其实是理解题意有误,因为每个位置上的数字表示的是最大的跳力而不是像玩大富翁一样摇骰子摇出几一定要走几。这里可以用贪婪算法 Greedy Algorithm,因为我们只对最远能到达的位置感兴趣,所以维护一个变量 reach,表示最远能到达的位置,初始化为0。而所有小于等于 reach 的位置都可以通过连续跳跃到达的,则只要 reach 大于等于最后一位置,就说明可以跳到最后一个位置。所以问题的核心就变成了尽可能的更新 reach 为最大值,这样就可以一次遍历数组中的每个位置,若这个位置小于等于 reach,说明是在可以到达的范围内,而从该位置可以到达的最大范围就是 i + nums[i],用这个最大范围来更新 reach。若某个时刻 reach 已经大于等于 n-1 了,说明可以到达最后的位置了,不需要进一步更新了,可以直接 break 掉循环。循环退出了之后,比较 reach 和 n-1 的只,若大于等于则返回 true,否则返回 false,参见代码如下: Github 同步地址: 类似题目: 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 打赏 ActivityMetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |