leetcode-55. Jump Game

来源:互联网 发布:智能手机自动开机软件 编辑:程序博客网 时间:2024/06/13 22:25

leetcode-55. Jump Game

题目:

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.

常规贪心算法思路,维护一个到目前为止到达最远的点Farthest,然后遍历Farthest之前的所有点。

public class Solution {    public boolean canJump(int[] nums) {        int Farthest = 0,pos=0;        while(pos<= Farthest){            Farthest = Math.max(nums[pos]+pos,Farthest);            if(Farthest>=nums.length-1) return true;            pos++;        }        return false;    }}
0 0
原创粉丝点击