首页 > 学院 > 开发设计 > 正文

LeetCode 55. Jump Game

2019-11-08 02:20:02
字体:
来源:转载
供稿:网友

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array rePResents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

answer:

class Solution {public:    bool canJump(vector<int>& nums) {        int start = 0,end = 0;        while(end < nums.size()){            int max = 0;            for(end = start;end < nums.size(); end ++){                if(nums[end] == 0) break;            }            if(end >= nums.size() -1) return true;            for(int i = start; i < end; i ++){                int temp = nums[i] - (end - i);                if(temp > max) max = temp;            }            if(max <= 0){                return false;            }            nums[end] = max;            start = end;        }        return true;    }};


上一篇:poj 2676 Sudoku(dfs)

下一篇:HashMap 源码解析

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表