lintcode : 跳跃游戏
跳跃游戏
给出一个非负整数数组,你最初定位在数组的第一个位置。
数组中的每个元素代表你在那个位置可以跳跃的最大长度。
判断你是否能到达数组的最后一个位置。
样例
A = [2,3,1,1,4],返回 true.
A = [3,2,1,0,4],返回 false.
解题
更新
public class Solution {
/**
* @param A: A list of integers
* @return: The boolean answer
*/
public boolean canJump(int[] A) {
// wirte your code here
if(A== null || A.length == 0)
return true;
int maxJump = A[0];
if(maxJump == 0 && A.length == 1)
return true;
if(maxJump == 0)
return false;
for(int i=1;i<A.length;i++){
maxJump = Math.max(maxJump - 1,A[i]); // 向前走一步 -1,并更新可走步长
if(maxJump >= A.length - i -1){ // 判断是否能够到达 len -1 位置
return true;
}
if(maxJump <=0)
return false;
}
return true; // false 都能通过
}
}
定义一个布尔数组,对每个位置判断其是否可以走
当数组的第一个位置是0的时候,返回false
初始布尔数组第一个位置是true,这样是为了保证走的连续型,当某个位置是false说明利用前面走的方式不能走到该位置,下面就无法走下去了。返回false
或者说,能到达当前位置的时候,当前位置才可以向前走,能到达当前位置体现在visited[i] = true
public class Solution {
/**
* @param A: A list of integers
* @return: The boolean answer
*/
public boolean canJump(int[] A) {
// wirte your code here
if(A == null || A.length<=1)
return true;
boolean visited[] = new boolean[A.length];
visited[0] = true;
if(A[0] ==0) return false;
for(int i = 0;i<A.length;i++){
if(visited[i] == false)
return false;
int d = A[i];
int jump = 0;
while(jump <=d){
if(jump+i< A.length)
visited[jump+i] = true;
if(jump+i == A.length -1)
return true;
jump++;
}
}
return visited[A.length - 1];
}
}
但是上面的程序在LeetCode无法通过,lintcode只是小样本数据
leetcode 看到不需要定义布尔数组的解法
定义变量maxJump:表示从现在位置能够向后走的最大的步数
显然maxJump受前一个maxJump的影响,前一个maxJump走到现在位置,只需要一步,走到现在 位置是maxJump -1
在 i 位置可以最远向后走的步数是 A[i]
两者的最大值就是 i 位置开始向后走的步数:maxJump = max( maxJump -1,A[i])
当maxJump ==0 的时候说明,不能向后走了,死局。
注意:对最后一步的maxJump可以为0,因为已经走到了最后了,所以for循环只判断的倒数第二个位置
public class Solution {
/**
* @param A: A list of integers
* @return: The boolean answer
*/
public boolean canJump(int[] A) {
// wirte your code here
if(A == null || A.length<=1)
return true;
if(A[0] ==0) return false;
int maxJump = A[0];
for(int i = 1;i<A.length -1;i++){
maxJump = Math.max(maxJump - 1,A[i]);
if(maxJump == 0)
return false;
}
return true;
}
}
lintcode : 跳跃游戏的更多相关文章
- lintcode: 跳跃游戏 II
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = ...
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode(45): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- 计蒜客-跳跃游戏二 (简单dp)
题目链接:https://nanti.jisuanke.com/t/20 跳跃游戏二 给定一个非负整数数组,假定你的初始 ...
- [Leetcode]44.跳跃游戏Ⅰ&&45.跳跃游戏Ⅱ
跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出 ...
- 跳跃游戏 12 · Jump Game 12
跳跃游戏 1 [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: 由于要用itera ...
- LeetCode:跳跃游戏【55】
LeetCode:跳跃游戏[55] 题目描述 给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.判断你是否能够到达最后一个位置. 示例 1: 输入: ...
- 运用NP求解 “跳跃游戏”---计蒜客
计蒜客里面有一道“跳跃游戏的问题” 给定一个非负整数数组,假定你的初始位置为数组第一个下标. 数组中的每个元素代表你在那个位置能够跳跃的最大长度. 你的目标是到达最后一个下标,并且使用最少的跳跃次数. ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- Linux android开发环境问题:Unexcepted exception:cannot run program "android-sdk-linux/platfor-tools/adb" :err=2,No such file or directory.
出现这个问题的原因: 我的linux是64位 ,而adb目前只有32位的,所以要安装运行32的环境. 不同的linux系统需要安装的不同: 我的Centos 解决方案如下 其他linux操作系统(参 ...
- AppDelegate中的方法解析
// 当应用程序启动完毕的时候就会调用(系统自动调用) -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOp ...
- Teamwork——Week4 团队项目之NABC
项目框架——NABC模型 一.N(Need需求) 我们组主要的用户对象是第三小组——UI小组的同学们,因此我们的用户需求就是他们的数据需求. 1)提供给UI小组整理好的数据库,和前一组讨论好数据结构. ...
- android开发 自定义图文混排控件
功能:图文混排,可自动缩放字体,如图: 单点触控使用的代码来自:http://blog.csdn.net/xiaanming/article/details/42833893 谢谢博主! 在该dem ...
- web前端网页特效大全导航列表
CSS3和Html5 图表与图形 表单验证 导航菜单 table选项卡 视频播放器 日期和时间 返回顶部 图层代码 滚动代码 幻灯片 文字特效 图片放大镜 juqery焦点图 瀑布流 广告悬浮代码 在 ...
- bzoj 1270 DP
w[i,j]代表高度j,第i颗树的时候的最大值 那么w[i,j]:=max(w[i,j+1],w[k,j+heigh])+sum[i,j]: 但是这样枚举是n^3的,我们发现转移的第二个选择w[k,j ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【POJ】【2449】Remmarguts' Date
K短路/A* 经(luo)典(ti) K短路题目= = K短路学习:http://www.cnblogs.com/Hilda/p/3226692.html 流程: 先把所有边逆向,做一遍dijkstr ...
- 【BZOJ】【2002】【HNOI2010】弹飞绵羊
呃这题的Hint写着splay启发式合并……但是蒟蒻不懂T_T只好写个简单的LCT来蒙混过关,就是时间效率上差劲的很…… 不过能够一次AC心情也是蛮愉悦的~ /******************** ...
- yield curve
1. A yield curve can be built using deposit rates, swap rates, and future/forward rates 2. A par-cou ...