Jump Game 解答

Question

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.

Solution

We apply Greedy Algorithm here to solve the problem. Key to the solution is to check largest distance that every element can reach.

1) when the current position can not reach next position (return false)

2) when the maximum index can reach the end (return true).

 1 public class Solution {
 2     public boolean canJump(int[] nums) {
 3         if (nums == null || nums.length <= 1)
 4             return true;
 5         int max = 0;
 6         for (int i = 0; i < nums.length; i++) {
 7             if (nums[i] == 0 && max <= i)
 8                 return false;
 9             if (i + nums[i] > max)
10                 max = i + nums[i];
11             if (max >= nums.length - 1)
12                 return true;
13         }
14         return false;
15     }
16 }
时间: 2024-11-06 12:25:55

Jump Game 解答的相关文章

leetcode 55 Jump Game ----- java

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 = 

leetcode 45 Jump Game II ---- java

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. Your goal is to reach the last index in the minimum number of jumps

[array] leetcode-55. Jump Game - Medium

leetcode-55. Jump Game - Medium descrition 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 abl

LeetCode403. Frog Jump

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord

PHP常见问题及解答

当作PHP学习时,总是会在baidu上查很多的例如开发环境的选择呀,PHP好不好呀!或者是不是转学JAVA,或是.NET等: 首先本人是从2010年下半年开始报名学的PHP(IN Guangzhou),每周一天学了近6个月左右,从最基础的HTML,CSS,DIV,JAVASCRIPT,AJAX,PHP,然后学二次开发:闲暇之余还开通了一个个人blog( PHP wordpress); 由于个人工作原因,这几年放了一段时间未动PHP了,今年开始又自学了.NET; ---目的就想业余做一份兼职,锻炼

微信送礼物投票系统的详细解答

就目前来说,市场上的第三方微信投票系统种类很多,功能不一鱼龙混杂,功能很多的情况下有一些细微的区别,对于用户来说选择有一定的难度,下面我就来简单介绍一下微信投票活动大家经常关注的16个问题,对此进行详细的解答:只要能同时包括这个些功能的系统,通常都能够很好的满足活动举办方的要求,活动良好的用户体验! Q1.该投票系统可以设置每个微信用户投票次数吗? A3:可以的,可以设置一次活动每个微信用户的投票数,可设置每个微信用户每天的投票数!并且取消关注自动减掉此用户投票的所有记录,做到了自动减票的功能.

2014马哥Linux0217中对0214三题的解答

前几天在做2014马哥Linux0214的作业的时候,发现其实这三题在0217中有解答,当然觉得马哥比自己写得好太多,所以忍不住要把马哥的答案贴出来,以供自己学习. 第一题:写一个脚本,用for循环实现显示/etc/init.d/functions./etc/rc.d/rc.sysinit./etc/fstab有多少行 #!/bin/bash for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab;do line

JAVA常见面试题及解答-java开发

JAVA常见面试题及解答 Java的垃圾回收总结  浅谈Java中的内部类 1)transient和volatile是java关键字吗? 如果用transient声明一个实例变量,当对象存储时,它的值不需要维持.例如: class T { transient int a;  //不需要维持 int b;  //需要维持 } 这里,如果T类的一个对象写入一个持久的存储区域,a的内容不被保存,但b的将被保存. volatile修饰符告诉编译器被volatile修饰的变量可以被程序的其他部分改变.在多

[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: