算法系列——Jump Game
来源:互联网 发布:java接收json对象数组 编辑:程序博客网 时间:2024/06/06 02:43
题目描述
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.
解题思路
贪心算法,我们应该维护一个局部最优值和全局最优解决。
对于数组 [2,3,1,1,4]
最终结果是true.
对于数组[3,2,1,0,4]
从表格中可以看到,在索引为3的位置,当前可达最大距离也就只能到这里,当索引为4时,返现4 大于最大可达距离,直接返回false.
程序实现
public class Solution { public boolean canJump(int[] nums) { int maxReach=0; for( int i=0;i<nums.length;i++){ //如果当前索引超出能够到达的最大位置 if(i>maxReach) break; maxReach=Math.max(nums[i]+i,maxReach); if(maxReach>=(nums.length-1)) return true; } return false; }}
阅读全文
0 0
- 算法系列——Jump Game
- 经典算法——Jump Game
- 贪心算法——jump-game
- 【LeetCode】 Jump Game 系列
- 数据结构与算法[LeetCode]——Jump Game I II
- 经典算法——Jump Game(II)
- LeetCode算法题——Jump Game II
- 算法练习(9)—— Jump Game II
- LeetCode——Jump Game
- leetcode——Jump Game
- LeetCode55——Jump Game
- LeetCode——Jump Game
- Leet code —Jump Game
- Algorithms—55.Jump Game
- jump game 贪心算法
- 贪心法、动态规划——jump-game,jump-gameII
- Jump Game II-贪心算法
- LeetCode Jump Game(贪心算法)
- 修改AfxMessageBox对话框标题
- Tkinter 简单用户登录注册程序
- Regularization and Bias/Variance
- Vim使用技巧
- 关于touchslider.js滑动切换页面的使用简介--注解版
- 算法系列——Jump Game
- 学习mybatis框架中遇到:org.apache.ibatis.exceptions.PersistenceException: 解决方法
- TCP/IP的四元组 五元组 七元组
- sklearn浅析(七)——Support Vector Machines
- UVa 11683
- oracle中的视图,序列和索引
- 单独数字II
- Web前端开发规范文档
- 图片剪切遇到的bug