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 ...
随机推荐
- Asp.NET网站Session浅谈
.NET网站在Web.config的<configuration>下<system.web>下<sessionState>配置session. sessionSta ...
- C++实现的哈希搜索
C++实现的哈希搜索 程序内容 Complete a text searching engine using hash table. 完成一个文本搜索引擎,使用哈希表 程序设计 程序流程图 程序代码 ...
- C++ STL find
find 函数,复杂度O(n) 涉及一些 泛型编程 #include <iostream> #include <string.h> #include <string> ...
- STL学习三:deque容器
1.Deque简介 deque是“double-ended queue”的缩写,和vector一样都是STL的容器,deque是双端数组,而vector是单端的. deque在接口上和vector非常 ...
- 微软职位内部推荐-Senior Software Engineer-SDP
微软近期Open的职位: Position: Senior SDE The R&D of Shared Data Platform at Application and Services Gr ...
- 四则运算2+psp0级表格
四则运算2 一.题目和要求 题目:写一个能自动生成小学四则运算题目的程序,要求一次输出不少于30道,只能是整数100以内的四则运算(四则运算1升级版) 要求: 1.题目避免重复 2.可定制(数量/打印 ...
- Spring MVC 环境搭建(二)
在Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面. 但这存 ...
- 【CentOs】搭建svn服务器
参考资料: svn攻略: http://blog.csdn.net/colinchan/article/details/1865154 错误解决:http://hi.baidu.com/anglem/ ...
- Java多线程——<七>多线程的异常捕捉
一.概述 为什么要单独讲多线程的异常捕捉呢?先看个例子: public class ThreadException implements Runnable{ @Override public void ...
- shiro中unauthorizedUrl不起作用
解决方法: 在shiro配置文件中添加(异常全路径做key,错误页面做value) <bean class="org.springframework.web.servlet.handl ...