{ "type": "code_options", "author": "https://github.com/begeekmyfriend/leetcode", "source": "solution.md", "exercise_id": "837128c8eb2c481ba2cb3ca390a5df10", "keywords": "贪心,数组,动态规划", "title": "跳跃游戏 II", "desc": [ { "content": "\n

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

\n

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

\n

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

\n

示例:

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

说明:

\n

假设你总是可以到达数组的最后一个位置。

", "language": "markdown" } ], "answer": [ { "content": "", "language": "cpp" } ], "prepared": [ [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ] ], "template": { "content": "#include \nusing namespace std;\nclass Solution\n{\npublic:\n\tint jump(vector &nums)\n\t{\n\t\tint steps = 0;\n\t\tint lo = 0, hi = 0;\n\t\twhile (hi < nums.size() - 1)\n\t\t{\n\t\t\tint right = 0;\n\t\t\tfor (int i = lo; i <= hi; i++)\n\t\t\t{\n\t\t\t\tright = max(i + nums[i], right);\n\t\t\t}\n\t\t\tlo = hi + 1;\n\t\t\thi = right;\n\t\t\tsteps++;\n\t\t}\n\t\treturn steps;\n\t}\n};", "language": "cpp" }, "node_id": "dailycode-a6c3e70f1c0045c1b2929f56cc5ce02f", "license": "csdn.net", "created_at": 1637894158, "topic_link": "https://bbs.csdn.net/topics/600469921" }