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.

Example

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

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

分析:

使用一个variable来保存当前剩余的“可跳的步数”,如果小于等于0,则表示不能再往下跳了。

 public class Solution {
/**
* @param A: A list of integers
* @return: The boolean answer
*/
public boolean canJump(int[] nums) {
if (nums == null) return false;
int stepsRemaining = ; for (int i = ; i < nums.length; i++) {
stepsRemaining = Math.max(stepsRemaining - , nums[i]);
if (stepsRemaining <= && i != nums.length - ) return false;
}
return true;
}
}

Jump Game II

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.

 
Example

Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

分析:

当我们在第一个点“2”的时候,我们需要遍历所有能够到达的点, i.e., 3, 1,然后找出当我们到达第3个点的时候,从哪个点(3或者1)起跳剩余跳数最多。

 public class Solution {

     public int jump(int[] A) {
if (A == null || A.length <= ) return ; int position = , totalJumps = ;
int remainingJumps = A[];
int jumps = A[]; while (position < A.length - ) {
// find max remaining jumps in the "jump range"
for (int i = ; i <= jumps; i++) {
if (position == A.length - ) return totalJumps;
remainingJumps = Math.max(A[position], remainingJumps - );
position++;
}
jumps = remainingJumps;
totalJumps++;
}
return totalJumps;
}
}

思路二:

假定我们从 index i 处起跳, 那么最多能够跳到 i + A[i] (代码中的currentFarthestPoint)这么远。那么我们需要在 i 到 i + A[i] 这个范围内找到一个点 p,使得如果我们从那个p点开始跳,它能够比任何一个在i 到 A[i] 这些点都能reach更远或者相等的点(代码中的nextFarthestPoint),并把那个最远的点作为下一次跳能够到达的最远点。

 public class Solution {
public int jump(int[] A) {
if (A == null || A.length <= ) return ;
int total = , currentFarthestPoint = A[], i = ;
while (i < A.length) {
int nextFarthestPoint = ;
while (i <= Math.min(A.length - , currentFarthestPoint)) {
nextFarthestPoint = Math.max(nextFarthestPoint, A[i] + i);
i++;
}
total++;
currentFarthestPoint = nextFarthestPoint;
}
return total;
}
}

Jump Game | & ||的更多相关文章

  1. [LeetCode] Frog Jump 青蛙过河

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  2. [LeetCode] Jump Game 跳跃游戏

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

  3. [LeetCode] Jump Game II 跳跃游戏之二

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

  4. Leetcode 45. Jump Game II

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

  5. Leetcode 55. Jump Game

    我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...

  6. LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]

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

  7. Leetcode jump Game II

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

  8. Leetcode jump Game

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

  9. bug report: Jump to the invalid address stated on the next line at 0x0: ???

    gdb或者vlagrind报告: ==14569== Jump to the invalid address stated on the next line ==14569== at 0x0: ??? ...

  10. Jump Game 的三种思路 - leetcode 55. Jump Game

    Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...

随机推荐

  1. java web中jsp,action,service,dao,po分别是什么意思和什么作用

    JSP:全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它[1] 是由Sun Microsystems公司倡导.许多公司参与一起建立的一种动 ...

  2. MVC2 Area实现网站多级目录

    Areas是ASP.NET Mvc 2.0版本中引入的众多新特性之一,它可以帮你把一个较大型的Web项目分成若干组成部分,即Area.实现Area的功能可以有两个组织形式: 在1个ASP.NET Mv ...

  3. angular自己的笔记

    angular知道怎么用了, 就打算读一读源代码; <html ng-app="phonecatApp"> <head> <meta charset= ...

  4. 深入研究Struts2(一)---Struts2是什么?它的工作原理是什么?

    本文绝对原创, 欢迎转载, 但是转载请记得注明文章出处:http://blog.csdn.net/izard999/article/details/39891281 近4年都在从事Android方 面 ...

  5. Yii2的view需要链接跳转

    添加 use yii\helpers\Url; view中的连接 <?= Url::toRoute('post/index');?>//post为你的当前控制器名,index为view模版

  6. firefox与chrome中对select下拉框中的option支持问题

    firefox可以直接修改option的字体样式,但是chrome中option的字体样式是继承select的,这个是在项目中遇到的,具体的可以看一下 http://www.cnblogs.com/r ...

  7. 以一个权限系统来告别WebForm —开篇

     前言: 当今是互联网的时代,我们己经阻止不了它的发展了,只有跟上脚步,才不会被抛弃,松散了这么久,该紧紧了.  背景: 我之所以说以一个权限应用系统来告别我的WebForm内部系统的生涯,是缘于我自 ...

  8. You've got to find what you love

    你必须找到你爱的东西 You've got to find what you love 史蒂夫乔布斯2005年6月在斯坦福大学毕业典礼上的演讲 I am honored to be with you ...

  9. maven 热部署成功案列

    首先配置tomcat-user.xml,这个文件是在tomcat的conf文件夹下面 在</tomcat-users>前添加这段 <role rolename="admin ...

  10. git分支与版本管理、版本回退、冲突解决记录

    一.基础使用 1.初始化本地仓库 git init 2.关联远程仓库 git remote add origin git@github.com:用户名/仓库名.git 3.添加远程仓库文件到本地 gi ...