leetCode--- Jump Game II
来源:互联网 发布:mysql 索引 性能提升 编辑:程序博客网 时间:2024/06/04 18:26
一. 题目: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.)
Note:
You can assume that you can always reach the last index.
二. 思路分析
题目大意:给定一组非负整数,您最初位于数组的第一个索引处。 数组中的每个元素代表您在该位置的最大跳转长度。思路比较简单:也使用贪心,只不过是要记录当前一跳所能到达的最远距离、上一跳所能达到的最远距离,和当前所使用跳数就可以了代码如下:
public int jump(int[] A) {int jumps = 0, curEnd = 0, curFarthest = 0;for (int i = 0; i < A.length - 1; i++) {curFarthest = Math.max(curFarthest, i + A[i]);if (i == curEnd) {jumps++;curEnd = curFarthest;}}return jumps;}
阅读全文
0 0
- LeetCode: Jump Game II
- LeetCode Jump Game II
- LeetCode: Jump Game II
- [Leetcode] Jump Game II
- [LeetCode] Jump Game II
- [Leetcode] Jump Game ii
- leetcode Jump Game II
- leetcode Jump Game II
- [LeetCode]Jump Game II
- [leetcode]Jump Game II
- LeetCode-Jump Game II
- [leetcode] Jump Game II
- LeetCode - Jump Game II
- LeetCode:Jump Game II
- LeetCode | Jump Game II
- 【Leetcode】Jump Game II
- LeetCode Jump Game II
- [LeetCode] Jump Game II
- SpringBoot-10整合AOP
- Hash (一)
- Elasticsearch Java虚拟机配置详解
- 小点
- Node.js模块导出exports 和 module.exports 的区别
- leetCode--- Jump Game II
- Java学习笔记--CSS笔记
- WEBBASIC Unit04&05 JavaScript 概述 、 JavaScript 基础语法 、 流程控制、 JavaScript对象概述 、 常用内置对象一 、 常用内置对象二 、 常用内置
- 创业成长系列.前端项目模块化解决方案
- CentOS下ELK的搭建
- testbench中的显示、打印任务的使用
- VS2015下添加Qt插件及路径配置
- 深入理解Java虚拟机重点
- let 和 const 命令