×
广告

Leetcode 45. Jump Game II

96
SnailTyan
2018.07.12 18:19* 字数 30

文章作者: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/
Leetcode
Web note ad 1