Java for LeetCode 055 Jump Game

时间:2022-11-13 09:28:35

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.

解题思路:

参考Java for LeetCode 045 Jump Game II把返回值改为boolean即可,JAVA实现如下:

 static public boolean canJump(int[] nums) {
int index=0,maxStepIndex=0,start=0;
while(nums.length>1){
for(int i=start;i<=index+nums[index];i++){
if(i+nums[i]>=nums.length-1)
return true;
if(i+nums[i]>=nums[maxStepIndex]+maxStepIndex)
maxStepIndex=i;
}
start=index+nums[index]+1;
if(index==maxStepIndex)
return false;
index=maxStepIndex;
}
return true;
}

Java for LeetCode 055 Jump Game的更多相关文章

  1. Java for LeetCode 045 Jump Game II

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

  2. &lbrack;LeetCode&rsqb; 45&period; Jump Game II 跳跃游戏 II

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

  3. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. maven中Rhino classes &lpar;js&period;jar&rpar; not found - Javascript disabled的处理

    想使用单元测试 来测一下服务请求,于是想到了使用Junit,查了一下,决定使用 HttpUnit 来发送请求 于是在maven中引入了 <dependency> <groupId&g ...

  2. mysql -B 恢复与不加

    -B 跟--database 意义一样 在默认不指定库时候 连续名称,只有第一个名称为库名,后面的都为表名 而使用 -B 或者 --database 之后 所有的名 都是库名 1 导出单个库时候加了- ...

  3. mokoid android open source HAL hacking in a picture

    /************************************************************************** * mokoid android HAL hac ...

  4. Android的一个自定义的动态添加Dialog类

    android里面会有自己内置的Dialog的提示框,也算是比较方便的了,但是为了省点时间,我们在项目里面添加了一个自己的Dialog类,这个类实现了能够动态的添加按钮和一些提示语句或者其他的显示效果 ...

  5. hadoop生态圈列式存储系统--kudu

    介绍 Kudu 是一个针对 Apache Hadoop 平台而开发的列式存储管理器.Kudu 共享 Hadoop 生态系统应用的常见技术特性: 它在 commodity hardware(商品硬件)上 ...

  6. &lbrack;PA2014&rsqb;Matryca

    [PA2014]Matryca 题目大意: 有一堵长度为\(n(n\le10^6)\)的墙需要刷漆,你有一把长度为\(k\)的刷子.墙和刷子都被均匀划分成单位长度的小格,刷子的每一格中都沾有某种颜色的 ...

  7. 问题11:web前端开发规范手册&lpar;转&rpar;

    一.规范目的 1.1  概述 ..................................................................................... ...

  8. EF 排序扩展

    public static class LinqOrderEx { private static IOrderedQueryable<T> OrderingHelper<T>( ...

  9. 详解ABBYY FineReader 12扫描亮度设置

    很多刚接触ABBYY FineReader 12的小伙伴可能出现过这样一个问题:在扫描过程中会显示一条消息以提示更改亮度设置.这是因为你 FineReader扫描设置中亮度未正确设置.下面小编就给小伙 ...

  10. 使用PL&sol;Scope分析PL&sol;SQL代码

    使用PL/Scope分析你的PL/SQL代码 从11g開始Oracle引入了PL/Scope 用于编译器收集PL/SQL程序单元的全部标识符(变量名.常量名.程序名等). 收集到的信息可通过一系列静态 ...