题解 | 跳跃游戏(一)

跳跃游戏(一)

https://www.nowcoder.com/practice/07484f4377344d3590045a095910992b

#include <iostream>
#include <vector>
using namespace std;


int main() {
    int n;
    cin >> n;
    vector<int> nums(n);
    for (int i = 0; i < n; ++i) {
        cin >> nums[i];
    }
    int maxReach = 0 ;
    for (int i = 0; i < nums.size(); ++i) {
        if (i > maxReach) {
            cout<<"false"<<endl;
            return 0;
        }
        maxReach = max(maxReach, i + nums[i]);
    }
    if(maxReach >= nums.size() - 1){
         cout <<  "true"   << endl;
    }
    else 
    cout <<  "false" << endl;
    return 0;
}

if (i > maxReach) {

cout<<"false"<<endl;

return 0;

}

maxReach = max(maxReach, i + nums[i]);

全部评论

相关推荐

10-13 13:49
南京大学 财务
饿魔:笑死我了,你简直是个天才
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务