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. 事务四大特征:原子性,一致性,隔离性和持久性(ACID)

    一.事务 定义:所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位. 准备工作:为了说明事务的ACID原理,我们使用银行账户及资金管理的案例进行分析. [sql] ...

  2. BACKBONE源代码解析

    //2014.11// Backbone.js 1.0.0 // (c) 2010-2013 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may b ...

  3. Java 缓存技术

    以下仅是对map对方式讨论.没有对点阵图阵讨论.作缓存要做以下2点:  1:清理及更新缓存时机的处理: . 虚拟机内存不足,清理缓存 .. 缓存时间超时,或访问次数超出, 启动线程更新 2:类和方法的 ...

  4. <supports-screens>的用法

    <supports-screens android:resizeable=["true"| "false"] android:smallScreens=[ ...

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

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

  6. poj 3261 二分答案+后缀数组 求至少出现k次的最长重复子序列

    #include "stdio.h" #define maxn 20010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int rank[ma ...

  7. Putty远程登录VMware虚拟机Linux(Ubuntu)

    安装SSH服务 Ubuntu默认并没有安装ssh服务,如果通过ssh链接ubuntu,需要自己手动安装ssh-server.判断是否安装ssh服务,可以通过如下命令进行: www.linuxidc.c ...

  8. 修改MYSQL 表中的字段属性

    1.登录数据库 >mysql -u root -p 数据库名称 2.查询所有数据表 >show tables; 3.查询表的字段信息 >desc 表名称; 4.1.修改某个表的字段类 ...

  9. Spring学习3—控制反转(IOC)基于Annotation(注解)的依赖注入实现

    一.依赖注入--手工装配—注解方式 在java代码中使用@Autowired或@Resource注解方式进行装配的前提条件是:   1.引入context命名空间 需要在xml配置文件中配置以下信息: ...

  10. ECSHOP管理员密码忘记了怎么办?

    ECSHOP管理员密码忘记了怎么办? ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-09-06   不小心在后台把管理员全部给清空了,闹的网站都无法登陆了?有 ...