和跳跃游戏I差不多~
class Solution {
public:
int jump(vector<int>& nums) {
const int N=1e4+10;
int dp[N]={
0};
for(int i=1;i<nums.size();i++)
{
dp[i]=0x3f3f3f3f;
}
//init操作
for(int i=0;i<nums.size();i++)
{
for(int j=1;j<=nums[i];j++)
{
if(i+j<nums.size())
{
dp[i+j]=min(dp[i+j],dp[i]+1);
}
}
}
return dp[nums.size()-1];
}
};
LeetCode45:跳跃游戏Ⅱ
2024-01-05 17:28:01 30 阅读