LeetCode OJ 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.


【题目分析】

A[i]的值代表如果到达 i 时可以向前跳的最大步长,判断从i = 0出发是否能到达数组的最后一个值。



【思路】

用distance记录当前i之前跳的最远距离。如果distance< i,即代表即使再怎么跳跃也不能到达i。当到达i时,A[i]+i,代表从i能跳最远的距离。max(distance,A[i]+i)代表能到达的最远距离。



【java代码】

 1 public class Solution {
 2     public boolean canJump(int[] nums) {
 3         if(nums == null || nums.length == 0) return true;
 4
 5         int distance = 0;
 6         for(int i = 0; i < nums.length && i <= distance; i++){
 7             distance = Math.max(nums[i]+i, distance);
 8         }
 9         return distance < nums.length -1 ? false : true;
10     }
11 }
时间: 2024-12-17 19:57:36

LeetCode OJ 55. Jump Game的相关文章

[Leetcode][Python]55: Jump Game

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 55: Jump Gamehttps://leetcode.com/problems/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

LeetCode OJ: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 = 

LeetCode OJ:Jump Game II(跳跃游戏2)

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

笔试题68. LeetCode OJ (55)

这个题是个复杂的跳台阶问题,主要是测试能否跳到最后,它不像之前那些跳台阶问题了,之前的不会考虑能否跳出去的问题.所以之前的程序用在这里很可能会导致死循环:我的主要思路是"贪心算法+筛选条件"来解题. 1.贪心算法主要是用来减少跳的次数 我们可以从当前位置(cur)可以跳的步数(N),以及当前位置所跳的范围内[cur~cur+N]中每个台阶能跳的最大距离:用这两者综合起来决定下一次应该跳的位置.有人会说这样还不会漏掉问题的正解呢?下面的判断条件可以说明该问题的. 2.筛选条件是用来过滤掉

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

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

LeetCode OJ - Symmetric Tree &amp;&amp; Same Tree

这两道题,大同小异. 我都是用BFS,在遍历的过程,判断结构是否相同/对称,值是否相同. 下面是AC代码: 1 /** 2 * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 3 * @param root 4 * @return 5 */ 6 public boolean isSymmetricRecursively(TreeNode root){ 7

LeetCode OJ - Distinct Subsequence

这道题采用动态规划,可是我一开始没有想到.后来参考了discuss中前辈的代码和思路,才想通的. 方法二是因为每一步只和上一步的内容相关,所以可以只用O(n)的空间复杂度. 下面是AC代码: 1 /** 2 * Solution DP 3 * we keep a m*n matrix and scanning through string T, 4 * p[i][j] means the number of distinct subsequence of S(0...j) equal to T(