solution.md 3.6 KB
Newer Older
1 2 3 4 5 6 7 8
# 跳跃游戏 II
<p>给定一个非负整数数组,你最初位于数组的第一个位置。</p>
<p>数组中的每个元素代表你在该位置可以跳跃的最大长度。</p>
<p>你的目标是使用最少的跳跃次数到达数组的最后一个位置。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong> [2,3,1,1,4]<strong><br />输出:</strong> 2<strong><br />解释:</strong> 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳&nbsp;1&nbsp;步,然后跳&nbsp;3&nbsp;步到达数组的最后一个位置。</pre>
<p><strong>说明:</strong></p>
<p>假设你总是可以到达数组的最后一个位置。</p>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
9
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    vector<int> nums = {2, 3, 1, 1, 4};
    int res;

    res = sol.jump(nums);

    cout << res;
    return 0;
}
```

## 答案
```cpp
class Solution
{
public:
    int jump(vector<int> &nums)
    {
        int res_min = 0;
        int end = 0;
        int longest_distance = 0;
        for (int i = 0; i < nums.size(); ++i)
        {
            longest_distance = max(longest_distance, nums[i]);

            if (i == end)
            {
                if (end != nums.size() - 1)
                {
                    ++res_min;
                    end = longest_distance;
                    if (longest_distance >= nums.size() - 1)
                        break;
                }
                else
                    break;
            }
        }
        return res_min;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    int jump(vector<int> &nums)
    {
        int steps = 0;
        int lo = 0, hi = 0;
        while (hi < nums.size() - 1)
        {
            int right = 0;
            for (int i = lo; i <= hi; i++)
            {
                right = max(i + nums[i], right);
            }
            lo = hi + 1;
            hi = right;
            steps++;
        }
        return steps;
    }
};
```

### B
```cpp
class Solution
{
public:
    int jump(vector<int> &nums)
    {
        if (nums.size() == 1)
            return 0;
        int steps = 0, oldIdx = 0;
        int nextJump = oldIdx + nums[oldIdx];
        if (nextJump >= nums.size() - 1)
            return steps + 1;

        while (oldIdx < nums.size())
        {
            int maxJump = 0, newIdx = oldIdx;
            for (int j = oldIdx; j <= oldIdx + nums[oldIdx]; j++)
            {
                int nextJump = j + nums[j];
                if (nextJump >= nums.size() - 1)
                    return steps + 2;
                if (j + nums[j] > maxJump)
                {
                    maxJump = j + nums[j];
                    newIdx = j;
                }
            }

            oldIdx = newIdx;
            steps++;
        }
    }
};
```

### C
```cpp
class Solution
{
public:
    int jump(vector<int> &nums)
    {
        int i, j, n = nums.size();
        if (n == 1)
            return 0;
        int ans = 0;
        for (i = 0; i < n;)
        {
            if (i == n - 1)
                break;
            if (i + nums[i] >= n - 1)
            {
                ans++;
                break;
            }
            else
            {
                int max = i + 1;
                for (j = 2; j <= nums[i]; j++)
                {
                    if (nums[max] + max <= nums[i + j] + i + j)
                        max = i + j;
                }
                ans++;
                i = max;
            }
        }
        return ans;
    }
};
```