Jump Game | & ||
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.
Example
A = [2,3,1,1,4]
, return true
.
A = [3,2,1,0,4]
, return false
.
分析:
使用一个variable来保存当前剩余的“可跳的步数”,如果小于等于0,则表示不能再往下跳了。
public class Solution {
/**
* @param A: A list of integers
* @return: The boolean answer
*/
public boolean canJump(int[] nums) {
if (nums == null) return false;
int stepsRemaining = ; for (int i = ; i < nums.length; i++) {
stepsRemaining = Math.max(stepsRemaining - , nums[i]);
if (stepsRemaining <= && i != nums.length - ) return false;
}
return true;
}
}
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.
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2
. (Jump 1
step from index 0 to 1, then 3
steps to the last index.)
分析:
当我们在第一个点“2”的时候,我们需要遍历所有能够到达的点, i.e., 3, 1,然后找出当我们到达第3个点的时候,从哪个点(3或者1)起跳剩余跳数最多。
public class Solution { public int jump(int[] A) {
if (A == null || A.length <= ) return ; int position = , totalJumps = ;
int remainingJumps = A[];
int jumps = A[]; while (position < A.length - ) {
// find max remaining jumps in the "jump range"
for (int i = ; i <= jumps; i++) {
if (position == A.length - ) return totalJumps;
remainingJumps = Math.max(A[position], remainingJumps - );
position++;
}
jumps = remainingJumps;
totalJumps++;
}
return totalJumps;
}
}
思路二:
假定我们从 index i 处起跳, 那么最多能够跳到 i + A[i] (代码中的currentFarthestPoint)这么远。那么我们需要在 i 到 i + A[i] 这个范围内找到一个点 p,使得如果我们从那个p点开始跳,它能够比任何一个在i 到 A[i] 这些点都能reach更远或者相等的点(代码中的nextFarthestPoint),并把那个最远的点作为下一次跳能够到达的最远点。
public class Solution {
public int jump(int[] A) {
if (A == null || A.length <= ) return ;
int total = , currentFarthestPoint = A[], i = ;
while (i < A.length) {
int nextFarthestPoint = ;
while (i <= Math.min(A.length - , currentFarthestPoint)) {
nextFarthestPoint = Math.max(nextFarthestPoint, A[i] + i);
i++;
}
total++;
currentFarthestPoint = nextFarthestPoint;
}
return total;
}
}
Jump Game | & ||的更多相关文章
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 45. Jump Game II
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 笔记系列13 Jump Game II [去掉不必要的计算]
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- bug report: Jump to the invalid address stated on the next line at 0x0: ???
gdb或者vlagrind报告: ==14569== Jump to the invalid address stated on the next line ==14569== at 0x0: ??? ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
随机推荐
- java.io.stream
1. package com.io.Stream; import java.io.*; public class NyFileInputStream1 { /** * 读取文件的streamIO * ...
- 【转】div居中代码 DIV水平居中显示CSS代码
原文地址:http://www.divcss5.com/rumen/r622.shtml 如何使用CSS让DIV居中显示,让div水平居中有哪些CSS样式呢? 需要的主要css代码有两个,一个为tex ...
- 利用HTML5的Video进行视频截图并保存到本地
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- PowerDesigner-如何导出建表sql脚本
1 按照数据库类型,切换数据库. Database-> Change Current DBMS... 2 生成sql脚本 Database -> Database Generation 的 ...
- shell expr的用法
root@tcx4440-03:~# var=$var+1root@tcx4440-03:~# echo $var3+1 要想达到预期结果,用下列三种方法: (1)let "var+=1&q ...
- 【codevs1014/1068】背包型动态规划
分析: 状态转移方程: v[j]=max(v[j],v[j-a[i]]+a[i]) (j ← tol downto a[i]) /* 作者:flipped 题目:p1014 装箱问题 */ #incl ...
- Linux数据包路由原理、Iptables/netfilter入门学习
相关学习资料 https://www.frozentux.net/iptables-tutorial/cn/iptables-tutorial-cn-1.1.19.html http://zh.wik ...
- TCP/IP详解 学习六
从ip层看路由表 选路策略 选路策略:决定把哪些路由放到路由表的规则. Ip执行选路机制,而路由守护程序则提供选路策略. Netstat –rn 打印路由表,如果没有-n命令会搜索配置文件,将网络地 ...
- <jsp:invoke fragment=""/>的理解和使用
在传统 JSP 中,想要实现页面布局管理比较麻烦,为了解决在 JSP 中布局的问题,出现了很多开源软件,比如 Apache Tiles 和 SiteMesh 就是其中比较优秀的.但是使用开源软件实现布 ...
- MyBatis查询传一个参数时报错:There is no getter for property named 'sleevetype' in 'class java.lang.Integer
用MyBatis进行查询,传入参数只有一个时(非Map)如int,报错 There is no getter for property named 'sleevetype' in 'class jav ...