Leetcode 45. Jump Game II

版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/81020402

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Jump Game II

2. Solution

class Solution {
public:
    int jump(vector<int>& nums) {
        int current = 0;
        int count = 0;
        while(current < nums.size() - 1) {
            count++;
            if(current + nums[current] >= nums.size() - 1) {
                break;
            }
            int distance = 0;
            int index = current + 1;
            int begin = current + 1;
            int end = current + nums[current];
            for(int i = begin; i <= end; i++) {
                if(nums[i] + i > distance) {
                    distance = nums[i] + i;
                    index = i;
                }
            }
            current = index;
        }
        return count;
    }
};

Reference

  1. https://leetcode.com/problems/jump-game-ii/description/

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试