leetCode 55.Jump Game(跳跃游戏) 解题思路和方法
2015-07-14 17:59
537 查看
Jump Game
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多了0的填充,有可能是无法到达最终点的。代码运用贪心思想,当无论怎么走只能走到0的时候返回false。具体代码如下:
public class Solution { public boolean canJump(int[] nums) { if(nums.length == 0) return false; int i = 0; //判断有没有0,没有0的肯定能达到 while(i < nums.length){ if(nums[i] == 0){ break; } i++; } //没有0,肯定能达到 if(i == nums.length){ return true; } i = 0; while(i < nums.length){ if(i + nums[i] >= nums.length - 1) return true; if(nums[i] == 0) return false; int max = 0; int index = 0; //下一步能前进最大的步骤 for(int j = i+1; j - i <= nums[i]; j++){ if(max < j - i + nums[j]){ max = j - i + nums[j]; index = j; } }//走到下一步的索引 i = index; } return true; } }
相关文章推荐
- HandleErrorAttribute 特性使用
- [转] 编写高效的 CSS 选择器
- C++编译器与链接器工作原理
- hdu 1052 Tian Ji -- The Horse Racing
- Spring分布式事务实现
- 把请求数据映射成model类
- sql中的Tran和C#中的TransactionScope介绍
- nagios监控iptables状态
- 路由转发原则
- OpenStack Keystone配置产生错误Access denied for user 'keystone'@'10.0.2.15' (using password: YES)") None N
- TeamViewer远程唤醒主机实战教程(多图)
- 新工作要做的事宜
- oracle 备份数据库对象(存储过程PROCEDURE,FUNCTION,VIEW,TRIGGER...)
- Stack集合、queue集合、hashtable集合
- Android开发:shape和selector和layer-list的(详细说明)
- Drools入门-----------环境搭建,分析Helloworld Drools5.0的xls文件转drl文件提升解析效率 使用BRMS的Tomcat6.0配置
- cocos2d-x 3.6 程序流程
- freeswitch与E1设备对接方法
- UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
- 对Java并发编程的几点思考