【leetcode】Jump Game I, II 跳跃游戏一和二
题目:
Jump Game I:
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
.
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.
For example:
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.)
Java代码例如以下:
public class Jump_Game { public static void main(String[] args) {
int[] A = { 2, 3, 1, 1, 4 };
int[] B = { 3, 2, 1, 0, 4 }; System.out.println(canJump(A));
System.out.println(canJump(B)); } public static boolean canJump(int[] A) {
boolean[] can = new boolean[A.length];
can[0] = true; for (int i = 1; i < A.length; i++) {
for (int j = 0; j < i; j++) {
if (can[j] && j + A[j] >= i) {
can[i] = true;
break;
}
}
}
return can[A.length - 1];
}
}
public class jump_game_2 { public static void main(String[] args) {
int[] A = { 2, 3, 1, 1, 4 };
int[] B = { 3, 2, 1, 0, 4 }; System.out.println(jump(A));
System.out.println(jump(B));
} public static int jump(int[] A) {
int[] steps = new int[A.length];
steps[0] = 0; for (int i = 1; i < A.length; i++) {
steps[i] = Integer.MAX_VALUE;
for (int j = 0; j < i; j++) {
if (steps[j] != Integer.MAX_VALUE && j + A[j] >= i) {
steps[i] = steps[j] + 1;
break;
}
}
}
return steps[A.length - 1];
}
}
【leetcode】Jump Game I, II 跳跃游戏一和二的更多相关文章
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode力扣45题 跳跃游戏 II
原题目: 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...
- LeetCode(45): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [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 ☆☆☆☆☆(跳跃游戏 2)
https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...
- 045 Jump Game II 跳跃游戏 II
给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...
- LeetCode:Jump Game I II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
随机推荐
- 关于cookie使用的一些问题
保存cookie后提取出来发现字符串是被编码过的,需要decodeURIComponent进行下解码才可以 设置cookie setCookie(c_name, value, expiredays) ...
- 外星人(bzoj 2749)
Description Input Output 输出test行,每行一个整数,表示答案. Sample Input 1 2 2 2 3 1 Sample Output 3 HINT Test< ...
- 【Eclpise】Eclipse中Tomcat启动失败或者是重启失败
经常在Eclipse中遇到这样的问题,tomcat重启之后失败,而且也停止不了.最好的解决办法就是用DOS命令杀死进程. 比如下面这种情况: 1.查看进程ID 用windows的netstat查看信 ...
- hdu 1077(单位圆覆盖问题)
Catching Fish Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- tomcat 项目迁移到jboss5.1
1.拷贝tomcat\bin目录下两个jar到项目lib 目的:解决jboss java.lang.NoClassDefFoundError: org/apache/juli/logging/LogF ...
- [AI开发]深度学习如何选择GPU?
机器推理在深度学习的影响下,准确性越来越高.速度越来越快.深度学习对人工智能行业发展的贡献巨大,这得益于现阶段硬件计算能力的提升.互联网海量训练数据的出现.本篇文章主要介绍深度学习过程中如何选择合适的 ...
- luogu P1879 [USACO06NOV]玉米田Corn Fields
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- JVM类加载机制————2
类加载机制的第一个阶段加载做的工作有: 1.通过一个类的全限定名(包名与类名)来获取定义此类的二进制字节流(Class文件).而获取的方式,可以通过jar包.war包.网络中获取.JSP文件生成等方式 ...
- 小程序使用wxParse插件解析html标签图片间距问题
转自:https://www.cnblogs.com/likun123/p/9543376.html 小程序解析html标签,就需要用到wxParse啦.但是在解析连续图片的时候,会发现图片之间会有间 ...
- 老毛桃winpe优盘启动系统个性修改全攻略
PE优盘系统也有很多:大白菜.老毛桃.深度.通用PE工具箱.U大师.电脑店……这些PE优盘系统大多都会捆绑软件安装.更改主页等,一不小心,你就中招.虽然有些是可以自己去取消,但是启动画面还是带有各种L ...