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 = [2,3,1,1,4], return true.

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

这道题是比较相对于jump2是比较简单的,只需要求出是否能都达到终点即可

第一次记录了最长距离maxlen,如果最远距离和当前位置一致。那么就不可到达终点,如果最远距离超过了长度-1,那么直接返回true,虽然ac,但是结果并不是很好。

public class Solution {
    public boolean canJump(int[] nums) {
        int maxlen = nums[0];
        int len = nums.length;
        if ( len < 2 )
            return true;
        if( maxlen == 0)
            return false;
        for( int i = 1;i<len;i++){
            maxlen = (i+nums[i])>maxlen?(i+nums[i]):maxlen;
            if( i == maxlen )
                return false;
            if( len-1 <= maxlen )
                return true;
        }
        return len<=maxlen-1?true:false;
    }
}

然后稍微修改了一下代码,速度有所提升。(还是很奇怪的是,同样的代码,每次提交的时候运行时间有可能不一致。)

public class Solution {
    public boolean canJump(int[] nums) {
        int maxlen = nums[0];
        int len = nums.length;
        if ( len < 2 )
            return true;
        if( maxlen == 0)
            return false;
        if( maxlen >= len-1)
            return true;
        for( int i = 1;i<len;i++){
            nowlen = i+nums[i];
            if( nowlen > maxlen ){
                maxlen = nowlen;
                if( len-1 <= maxlen )
                    return true;
            }
            else if( i == maxlen )
                return false;

        }
        return true;
    }
}

看了官方的解答方案之后,发现还有一种更简便的写法,就是从后往前便利。虽然时间复杂度和空间复杂度没有变化,但是代码量少了不少,但是很奇怪的是,就算是官方答案,偶尔也会ac不了,出现超时,还有就是这两种答案的时间是一样的。

public class Solution {
    public boolean canJump(int[] nums) {
        int lastPos = nums.length - 1;
        for (int i = nums.length - 1; i >= 0; i--) {
            if (i + nums[i] >= lastPos) {
                lastPos = i;
            }
        }
        return lastPos == 0;
    }
}
时间: 2024-10-03 03:24:37

leetcode 55 Jump Game ----- java的相关文章

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

LeetCode: 55. Jump Game(Medium)

1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的值代表每一次你能从当前位置跳跃的步数.问能否跳到该数组的最后一个元素位置 注意:可以跳的步数超出数组长度依旧视为可以达到最后位置 3. 解题思路 从第一个元素开始遍历,记录下你所能到达的最远位置,例如{2, 2, 0, 1, 2},遍历第一个元素时,你所能到达的最远位置是"i+nums[i]&quo

[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 之 LRU Cache Java实现

LeetCode刷了41道题了,流程是按照戴兄的小书,很多不会的是参考Kim姐的代码,自己用Java抠腚的. 前几天做到了LRU Cache: C++的实现方法大同小异,大都用的是一个list加一个hash,hash中存储list节点地址,每次get从hash中寻key,有则将list相应节点放到链表头,没有则返回-1:每次set先判断hash中是否存在,存在则将相应节点移到表头,重置value值,如果不存在,判定长度是否达到预设的capacity,如果达到,删除表尾节点,新节点插入表头. 但是

BFS问题-LeetCode 55、45、5297、127、433、434(BFS)

[LeetCode #55]跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置. class Solution { public: bool canJump(vector& nums) { int maxReach = nums[0];

【LeetCode】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】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

Leetcode: Longest Palindromic Substring. java

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划public class Solution { public String longestPalindrome(String s) { if