LeetCode: 55. Jump Game(Medium)

1. 原题链接

https://leetcode.com/problems/jump-game/description/

2. 题目要求

给定一个整型数组,数组中没有负数。从第一个元素开始,每个元素的值代表每一次你能从当前位置跳跃的步数。问能否跳到该数组的最后一个元素位置

注意:可以跳的步数超出数组长度依旧视为可以达到最后位置

3. 解题思路

从第一个元素开始遍历,记录下你所能到达的最远位置,例如{2, 2, 0, 1, 2},遍历第一个元素时,你所能到达的最远位置是“i+nums[i]”=2,也就是nums[2]。

然后让 “i<2;i++”继续遍历,nums[1]+1=3,可以到达nums[3] =1,nums[3] +1>nums.length-1,返回true

4. 代码实现

public class JumpGame55 {
    public static void main(String[] args) {
        int[]nums ={2,2,0,2,4};
        System.out.println(canJump(nums));
    }
    public static boolean canJump(int[] nums) {
        int dis = 0;
        for (int i = 0; i <= dis; i++) {
            dis = Math.max(dis, i + nums[i]);
            System.out.println(dis);
            if (dis >= nums.length-1) {
                return true;
            }
        }
        return false;
    }
}

  

原文地址:https://www.cnblogs.com/huiAlex/p/8343593.html

时间: 2024-10-08 01:45:26

LeetCode: 55. Jump Game(Medium)的相关文章

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 (23) 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:18. 4Sum(Medium)

1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个数之和等于目标整数target.请找出所有满足此条件的四个整数. 3. 解题思路 先对nums进行排序,然后采用两层for循环来确定前两个数字,最后在第二层for循环中确定后两个数字. 注意可能存在重复解!! 如下图所示,对Input先进行排序:[-4, -1, -1,0, 1,2],target

LeetCode 495. Teemo Attacking(medium)

In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to outp

[LeetCode] Longest Consecutive Sequence(DP)

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation

原文:背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation [源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合类) SemanticZoom ISemanticZoomInformation 示例1.SemanticZoom 的示例Controls/CollectionControl/SemanticZoomDemo/SemanticZoomDemo.xaml <Page

LeetCode OJ——练习笔记(1)Evaluate Reverse Polish Notation

院招终于开始了,然后期待与兴奋过后却是面临着笔试一次又一次的失败,然后开始留意到LeetCode. 也想自己去体验一下诸多大牛通向无限coding路上都攻克过的一关. 话不多说,贴出原题: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expres

[LeetCode] Palindrome Partitioning II (DP)

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa"

LeetCode之Easy篇 ——(13)Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 思路分析: 1.熟悉罗马数字的规则.见LeetCode之Easy篇 --(12)Integer to Roman 2.将输入的罗马数字转化成数组,并逐一通过case比对,然后根据其规则进行运算. Java代码示例: class Solution { public int romanT