[leetcode]Jump Game
2015-07-19 10:54
337 查看
问题叙述性说明:
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.
基本思路:
从每一个可达的节点产生新的可达节点。依次类推。但用一个变量head记录当前可达最远的位置。对于i+A[i] < head 能够剪枝。
请见代码。
代码:
bool canJump(int A[], int n) { //C++ vector<int> record(n,0); record[0] = 1; if(n == 1) return true; int head = 0; for(int i =0; i < n; i++) { if(record[i] == 1 && i+A[i] > head) { for(int j = head-i+1; j <= A[i]; j++) { if(i+j == n-1) return true; record[i+j] = 1; } head = i+A[i]; } } return false; }
相关文章推荐
- Spring MVC 表单提交
- php标签<?php ?>中的结束标签是否有必要添加
- [LeetCode]233.Number of Digit One
- [PHP开发] 关于header setcookie session_start 3个函数的怪问题
- Android 之 ServiceManager与服务管理
- BAT批处理
- HDU 1561 The more, The Better (树形DP+背包)
- 递归数的计算
- Hadoop—在Eclipse中安装Hadoop插件及测试(Linux操作系统)--练习6
- 少年,你还太弱,请专心练剑
- 默认样式添加
- IOS 消息推送原理及实现总结
- ASP.NET MVC 之控制器与视图之间的数据传递
- UITableView性能优化(复用池的使用原理)
- iOS切换到UiScrollView界面卡死
- ios中URL字符串的URLencode与URLdecode
- POJ1002
- java面向对象(内部类)
- 技术随笔九——让我们慢慢地行走呵
- [算法系列之三十二]1的数目