[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.
Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
题意:
思路:
以 [2,3,1,1,4]为例

可以想象一只青蛙,在每个刻度的石头上,弹跳力分别为2,3,1,1,4。青蛙是否能安全过河。
用maxReachIdx记录当前能跳的最远距离
指针i边扫边更新maxReachIdx
若maxReachIdx >= nums.length - 1 则返回true。
例子走一遍,先初始化指针 i= 0 , maxReachIdx = 0

当 i = 0 时,进入for循环,maxReachIdx更新为 3, 可以把红色部分看为青蛙可跳的势力范围

当 i = 4, maxReachIdx仍然为 3 则maxReachIdx < nums.length - 1 返回false 。
代码:
public boolean canJump(int[] nums) {
int maxReachIdx = 0;
for (int i = 0; i < nums.length && i <= maxReachIdx; i++){
maxReachIdx = Math.max(maxReachIdx, i + nums[i]);
}
return maxReachIdx >= nums.length - 1;
}
[leetcode]55. Jump Game青蛙跳(能否跳到终点)的更多相关文章
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 55. Jump Game
我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...
- [LeetCode] 55. Jump Game 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
随机推荐
- 对JVM的简单了解
- Java继承(上)
继承的定义 在人类生活中的学徒关系,父子关系等等都属于继承: 但是在程序中是一种类与类的关系 在程序中可以使用父类的方法,也可以添加自己的方法,但是不能选择的继承,要继承就需要把所有的特点全部继承下来 ...
- 使用lite-server
进入项目根目录,执行下列步骤 安装lite-server npm install lite-server 新建配置文件bs-config.json bs-config.json中可以: 指定监听的端口 ...
- xterm下字体设置
code ~/.Xdefaults xterm*locale: true xterm.utf8: true xterm*utf8Title: true ! 滚动条 !XTerm*scrollBar: ...
- Android - 简单listview
//MainActivity.java package com.example.zc.listviewdemo; import android.support.v7.app.AppCompatActi ...
- Group Pathfinding & Movement in RTS Style Games
转自:http://gamasutra.com/blogs/AndrewErridge/20180522/318413/Group_Pathfinding__Movement_in_RTS_Style ...
- 安装ORACLE高可用RAC集群11g校验集群安装的可行性输出信息
安装ORACLE高可用RAC集群11g校验集群安装的可行性输出信息 作者:Eric 微信:loveoracle11g [grid@node1 grid]$ ./runcluvfy.sh stage - ...
- js中记住密码功能
js中记住密码功能(在前端实现) 直接上例子(如果你也要实现的话注意改一些变量名称,jsp代码不包含样式) Jsp代码: <form class="am-form tpl-form-l ...
- 杰克.多西 twitter创始人 必做清单和不必做清单
必做清单 活在当下 接受脆弱(Be vulnerable) 只喝柠檬水和红酒 每天 6 组下蹲和俯卧撑 每天跑步 3 英里 每天思考本清单 站直了 打拳击沙袋 10 分钟 跟所有人打招呼 每天 7 小 ...
- leetcode84
public class Solution { public int LargestRectangleArea(int[] hist) { // The main function to find t ...