欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

55. Jump Game

发布时间:2024/8/26 编程问答 72 豆豆
生活随笔 收集整理的这篇文章主要介绍了 55. Jump Game 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

description:

看是否能跳到最后.
Note:

Example:

Example 1:Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.Example 2:Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximumjump length is 0, which makes it impossible to reach the last index.

answer:

class Solution { public:bool canJump(vector<int>& nums) {int n = nums.size(), reach = 0;for (int i = 0; i < n; i++) {if (i > reach || reach >= n - 1) break;reach = max(reach, i + nums[i]);}return reach >= n - 1;} };

relative point get√:

hint :

就是一直维护一个最远距离

转载于:https://www.cnblogs.com/forPrometheus-jun/p/11273270.html

总结

以上是生活随笔为你收集整理的55. Jump Game的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。