leetcode — jump-game
/**
* Source : https://oj.leetcode.com/problems/jump-game/
*
* Created by lverpeng on 2017/7/17.
*
* 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.
*/
public class JumpGame {
/**
* max记录目前能到的最远位置,循环数组,每次max和当前i能到的最远位置作比较,如果比max大,说明能到比原来到更远的位置,更新max
* 如果max已经大于等于数组长度,则说明能到数组末尾,返回true
* 如果当前已经大于max说明当前位置已经到不了,返回false
*
* 如果上面没有返回,说明不能到数组末尾
*
* @param arr
* @return
*/
public boolean canJump (int[] arr) {
if (arr.length <= 0) {
return false;
}
int length = arr.length;
// 记录最远能到的位置
int max = 0;
for (int i = 0; i < max && i < length - 1; i++) {
if (i + arr[i] > max) {
max = i +arr[i];
}
if (max >= length - 1) {
return true;
}
}
return false;
}
public static void main(String[] args) {
JumpGame jumpGame = new JumpGame();
int[] arr = new int[]{2,3,1,1,4};
int[] arr1 = new int[]{3,2,1,0,4};
int[] arr2 = new int[]{3,2,1,0,4,1};
System.out.println("true---->" + jumpGame.canJump(arr));
System.out.println("false--->" + jumpGame.canJump(arr1));
System.out.println("false--->" + jumpGame.canJump(arr2));
}
}
leetcode — jump-game的更多相关文章
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [leetcode]Jump Game @ Python
原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: Given an array of non-negative integers, you ar ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode: Jump Game Total 解题报告
Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of ...
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- ubuntu installs matlab2017a
cd mkdir matlab sudo mount -o loop *1.iso matlab sudo ./matlab/install ... sudo mount -o loop *2.iso ...
- JS中encodeURI()、decodeURI()、encodeURIComponent()和decodeURIComponent()编码与解码
编码解码问题. 解决这个问题大家一般都使用encodeURI或者encodeURIComponent方法,在这里做一下总结: 首先看看各个方法不同浏览器的支持程度 函数 描述 FF N IE deco ...
- spring整合mybatis框架
1.导入jar包 2.配置文件 a. applicationContext.xml文件 <beans xmlns="http://www.springframework.org/ ...
- ESP32随笔汇总
版权声明:本文为博主原创文章,未经博主本人不得转载.联系邮箱:mynoticeable@gmail.com 1.ubuntu 14.04下搭建esp32开发环境 2.UBUNTU14.0.4安装ecl ...
- Android-finished with non-zero exit value 2
网上都是说,由于导入的依赖出现重复造成的,或者说 由于buildtools版本太高造成的,而我遇到的这个问题,这种两种方式无法去解决,所以才有了一下这种解决方式: 第一步,打开项目最外层的 build ...
- SQL Server对比两字符串的相似度(函数算法)
一.概述 最近有人问到关于两个字符串求相似度的函数,所以就写了本篇文章,分别是“简单的模糊匹配”,“顺序匹配”,“一对一位置匹配”.在平时的这种函数可能会需要用到,业务需求不一样,这里只给出参照 ...
- 快速找出网站中可能存在的XSS漏洞实践(一)
一.背景 笔者最近在慕课录制了一套XSS跨站漏洞 加强Web安全视频教程,课程当中有讲到XSS的挖掘方式,所以在录制课程之前需要做大量实践案例,最近视频已经录制完成,准备将这些XSS漏洞的挖掘过程记录 ...
- Android NDK学习(一):目录结构解析
我们配置好NDK环境后,基本可以看到其目录结构如下图: 下面我们来分析一下NDK根目录的结构: ndk-build:该Shell脚本是Android NDK构建系统的起始点,一般在项目中仅仅执行这一个 ...
- h5面试题
一.匿名函数实现阶乘第一种写法: 43 > F = fun(Func, 1) -> 1;43 > (Func, N) -> N * Func(Func, N - 1) end. ...
- 第87节:Java中的Bootstrap基础与SQL入门
第87节:Java中的Bootstrap基础与SQL入门 前言复习 什么是JQ? : write less do more 写更少的代码,做更多的事 找出所有兄弟: $("div" ...