zoukankan      html  css  js  c++  java
  • 45. 跳跃游戏 II

    给定一个非负整数数组,你最初位于数组的第一个位置。

    数组中的每个元素代表你在该位置可以跳跃的最大长度。

    你的目标是使用最少的跳跃次数到达数组的最后一个位置。

    示例:

    输入: [2,3,1,1,4]
    输出: 2
    解释: 跳到最后一个位置的最小跳跃数是 2。
      从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。

    int ans = 0;
    class Solution {
    public:
        int jump(vector<int>& nums) {
            int siz  = nums.size();
            ans=1;
            if(siz==1)return 0;
            int maxx = nums[0], stpnow=0;
            if(maxx>=siz-1)return ans;
            while(stpnow<siz-1){
                int maxxid=0;
                for(int i=stpnow;i<=maxx;++i){
                    maxxid=max(maxxid,i+nums[i]);
                }
                ++ans;
                stpnow = maxx;
                if(maxxid>=siz-1)break;
                maxx=maxxid;
            }
            return ans;
        }
    };
    

      

  • 相关阅读:
    POJ
    FZU
    HDU
    HDU
    HDU
    HDU
    Educational Codeforces Round 84 E. Count The Blocks
    B Boundary(由弦求圆)
    D. Maximum Sum on Even Positions(翻转1次,求最大偶数位和)
    E. DeadLee(思维,拓扑图处理)
  • 原文地址:https://www.cnblogs.com/DreamKill/p/13230213.html
Copyright © 2011-2022 走看看