leetcode45. 跳跃游戏 II

leetcode45. 跳跃游戏 II

题解:典型BFS

class Solution {
public:
    int jump(vector<int>& nums) {
        int ans = 0;
        int endPos = 1;
        int startPos = 0;
        while(endPos < nums.size()) {
            int maxDest = 0;
            for (int i = startPos; i < endPos; i++) {
                maxDest = max(maxDest, i + nums[i]);
            }
            startPos = endPos;
            endPos = maxDest + 1;
            ans++;
        }
        return ans;
    }
};

你可能感兴趣的:(leetcode,【算法】BFS,数据结构)