【Leetcode】经典的Jump Game in JAVA
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.
这道题的巧妙之处在于,每个值能够跳“值”大小步数。所以我们的思路例如以下:
1.记录到如今为止能跳到最远的距离,记为max
2.每个点能走的步数,记为step,且每往前面走一步。step--
3.推断到这个点后往后跳的步数是不是大于max,假设是就更新max,不是就继续往前走
这种话我们能够看到:假设前面这个点是零,且这个时候step步数不够你迈过去,那么就会自己主动跳出返回false。
可是假设能够一直这么走走到终点,那么返回true
package testAndfun;
public class jumpGame {
public static void main(String args[]){
int[] A = {3,0,0,0};
jumpGame jp = new jumpGame();
if(jp.canJump(A)) System.out.println("We win");
else System.out.println("we lose");
}
public boolean canJump(int[] A) {
int max = 0;
int step = 1;
if(A.length<=1) return true;
if(A[0]==0&&A.length>1) return false;
for(int i=0;i<A.length;i++){
step--;
if(i+A[i]>max){
max = i+A[i];
step = A[i];
}
if(step==0 && i<A.length-1) return false;
}
return true;
}
}
【Leetcode】经典的Jump Game in JAVA的更多相关文章
- 【一天一道LeetCode】#55. Jump Game
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- [leetcode] 403. Frog Jump
https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...
- leetcode面试准备: Jump Game II
1 题目 Given an array of non-negative integers, you are initially positioned at the first index of the ...
- leetcode面试准备: Jump Game
1 题目 Given an array of non-negative integers, you are initially positioned at the first index of the ...
- [Leetcode][048] Rotate Image 略详细 (Java)
题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有 ...
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- LeetCode OJ 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- ORACLE 检索某列包含特定字符串的数据表工具存储过程
使用示例: delete APPS.FIND_RESULT; set serveroutput ondeclare v_ret varchar(200);begin apps.sp_f ...
- kali2018 安装****
1.安装需要的依赖包: apt-get install qt5-qmake qtbase5-dev libqrencode-dev libappindicator-dev libzbar-dev ro ...
- C#-dynamic参考
dynamic 类型的作用是绕过编译时类型检查,改为在运行时进行解析. dynamic 类型简化了对 COM API(例如 Office Automation API).动态 API(例如 IronP ...
- 九度oj 题目1528:最长回文子串
题目描述: 回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串. 回文子串,顾名思义,即字符串中满足回文性质的子串. 给出一个只由小写英文字符a,b,c...x, ...
- 数组dome
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 解开Future的神秘面纱之任务执行
此文承接之前的博文 解开Future的神秘面纱之取消任务 补充一些任务执行的一些细节,并从全局介绍程序的运行情况. 任务提交到执行的流程 前文我们已经了解到一些Future的实现细节,这里我们来梳理一 ...
- BZOJ1221 [HNOI2001] 软件开发 【费用流】
题目 某软件公司正在规划一项n天的软件开发计划,根据开发计划第i天需要ni个软件开发人员,为了提高软件开发人员的效率,公司给软件人员提供了很多的服务,其中一项服务就是要为每个开发人员每天提供一块消毒毛 ...
- BZOJ2561 最小生成树 【最小割】
题目 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在加入一条边权为L的边(u,v),那么需要删掉最少多 ...
- 【CCF】通信网络 简单搜索
去重!不然有环就直接挂掉了...0分 #include<iostream> #include<cstdio> #include<string> #include&l ...
- 转载:lua和c的交互
extern "C" { #include "lua.h" #include "lualib.h" #include "lauxl ...