Leetcode 055 Jump Game(遍历)

xiaoxiao2021-02-28  35

题目连接:Leetcode 055 Jump Game

解题思路:从左向右遍历,维护可达到的最远位置。

class Solution { public: bool canJump(vector<int>& nums) { int p = 0, n = nums.size(); for (int i = 0; i < n; i++) { if (i <= p) p = max(p, i + nums[i]); else return false; } return true; } };
转载请注明原文地址: https://www.6miu.com/read-2621871.html

最新回复(0)