题目:

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 跳跃游戏一和二的更多相关文章

  1. [LeetCode] 45. Jump Game II 跳跃游戏 II

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

  2. 【LeetCode每天一题】Jump Game II(跳跃游戏II)

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

  3. Leetcode力扣45题 跳跃游戏 II

    原题目: 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...

  4. LeetCode(45): 跳跃游戏 II

    Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...

  5. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  6. [Leetcode] jump game ii 跳跃游戏

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

  7. [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 ...

  8. 045 Jump Game II 跳跃游戏 II

    给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...

  9. LeetCode:Jump Game I II

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

随机推荐

  1. C语言的二次实验报告

    题目一 题目二 题目三 题目四 题目五 部分源代码 11-1求矩阵的局部极大值 #include<stdio.h>int main(){    int m,n,i=0,j=0,count= ...

  2. python作为计算器(数学用法)

    1.基本的加减乘除与取余运算 >>> print(5+10) 15 >>> print(5-10) -5 >>> print(5*10) 50 & ...

  3. 15深入理解C指针之---内存释放

    一.手动申请的内存,必须及时进行内存释放,否则容易造成内存泄露.主要代码形式为: #include <stdio.h> #include <stdlib.h> int main ...

  4. 模块化开发(seajs)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. codeforces gym 100825 D Rings

    这题果然就是个暴力题.... 看每个T的四个方向,有'.',或者在边界上就填1 不然就填四个方向上最小的那个数再加1 然而写wa了几发,有点蠢... #include <bits/stdc++. ...

  6. echarts 金字塔

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. ASP.NET MVC Identity 使用自己的SQL Server数据库

    之前在网上看到的一篇后来找不到了,现在自己记录一下. 1.在web.config中添加一个数据库连接. <add name="dataContext" connectionS ...

  8. POJ 2253 Frogger Floyd

    原题链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  9. arcgis andriod Edit features

    来自:https://developers.arcgis.com/android/guide/edit-features.htm#ESRI_SECTION1_56C60DB71AF941E98668A ...

  10. 【面试 IO】【第十一篇】 java IO

    1.什么是比特(Bit),什么是字节(Byte),什么是字符(Char),它们长度是多少,各有什么区别 1>Bit最小的二进制单位 ,是计算机的操作部分 取值0或者1 2>Byte是计算机 ...