题目:

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];}
}

转载于:https://www.cnblogs.com/blfbuaa/p/6793026.html

【leetcode】Jump Game I, II 跳跃游戏一和二相关推荐

  1. 贪心法—— LeetCode45 跳跃游戏II(跳跃游戏进阶版)

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

  2. 高频leetcode动态规划部分:55. 跳跃游戏

    55. 跳跃游戏 难度中等1615 给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 . 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个下标. 示例 1: ...

  3. Leetcode刷题笔记 55. 跳跃游戏

    55. 跳跃游戏 时间:2020年12月1日 知识点:贪心 题目链接:https://leetcode-cn.com/problems/jump-game/ 题目 给定一个非负整数数组,你最初位于数组 ...

  4. leetcode 1345. Jump Game IV | 1345. 跳跃游戏 IV(BFS)

    题目 https://leetcode.com/problems/jump-game-iv/ 题解 好久没做 hard 了,今天时间多,挑战一下.用 lqy 同学的话说,这题叫做 "苦难题& ...

  5. 45. Jump Game II(跳跃游戏II)

    原题链接:https://leetcode.com/problems/jump-game-ii/ 这道题比较适合用贪心来做,局部的最优解最后刚好是整体的最优解. 如下图,开始的位置是 2,可跳的范围是 ...

  6. leetcode 1306. Jump Game III | 1306. 跳跃游戏 III(BFS)

    题目 https://leetcode.com/problems/jump-game-iii/ 题解 正如 hint 所说: 用 BFS,跳过的就不跳了,直到全部跳过,即 count == arr.l ...

  7. 跳跃游戏 I - VII

    跳跃游戏 [55. 跳跃游戏](https://leetcode.cn/problems/jump-game/) [45. 跳跃游戏 II](https://leetcode.cn/problems/ ...

  8. Leetcode 45. 跳跃游戏 II

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

  9. leetcode 45. 跳跃游戏 II 46. 全排列

    leetcode 45. 跳跃游戏 II 46. 全排列 45. 跳跃游戏 II 难度中等1273收藏分享切换为英文接收动态反馈 给你一个非负整数数组 nums ,你最初位于数组的第一个位置. 数组中 ...

最新文章

  1. 表单标签form的简单使用
  2. 12_信息熵,信息熵公式,信息增益,决策树、常见决策树使用的算法、决策树的流程、决策树API、决策树案例、随机森林、随机森林的构建过程、随机森林API、随机森林的优缺点、随机森林案例
  3. [转] Transformer图解
  4. java word在线预览_java 生成word文档并且在线预览的问题
  5. 计算机科学与技术专业实训手册,计算机科学与技术专业实习计划
  6. 重访火星时代网_huadingjin_新浪博客
  7. 谷歌地球下载及功能介绍
  8. bootstrap之文字排版
  9. ios下input框 数字键盘问题
  10. 关于T51的25C°电池曲线合成(MTK)
  11. 计算机英语构词法,计算机专业英语的构词方法
  12. how2heap(5):tcache_poisoning 2.31
  13. 统计学,机器学习,深度学习,数据挖掘的联系
  14. DotCode二维码基本概念
  15. 简单好用的ISD1820语音录放芯片
  16. STM32中的常量数组的首地址如何指定FLASH的某一个地址
  17. 四大CPU体系结构ARM、X86/Atom、MIPS、PowerPC
  18. Mac 10.15之后版本系统Gomonkey错误解决
  19. ant批量修改文件名_Ant Renamer-Ant Renamer(免费批量重命名工具)下载 v2.12官方版--pc6下载站...
  20. 快递取件码生成软件_智能快递箱:手机扫码1分钟取件 短信生成密码一柜一码

热门文章

  1. C语言实验大纲2010答案,C语言试验大纲(2010年修订).doc
  2. java instanceof 原理_java-在现代JVM实现中如何实现instanceof?
  3. android url webview,android - webview获取到当前页面的url
  4. mysql存储过程queue_mysql – 在Sequelize中调用输入/输出类型存储过程
  5. mysql self join_mysql self join的实现--left join 和inner join
  6. (传送门)linux命令总结dd命令详解
  7. 【技术分享】linux各种一句话反弹shell总结——攻击者指定服务端,受害者主机(无公网IP)主动连接攻击者的服务端程序(CC server),开启一个shell交互,就叫反弹shell。...
  8. ES业界优秀实践案例汇总
  9. spark submit 入门
  10. AttributeError: module 'jwt' has no attribute 'ExpiredSignatureError'