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 ...
随机推荐
- java基本类型的默认值
基本类型 默认值 取值范围 (最大/最小) 字节数 二进制位数 byte 0 127(2^7-1) -128(-2^7) 1byte 8bit short 0 32767(2^15 - 1) -327 ...
- python模块:csv
""" csv.py - read/write/investigate CSV files """ import re from _csv ...
- JavaScript基础视频教程总结(051-060章)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 【原创】IO流:读写操作研究(输入流)
默写代码(以下问题要求能默写,不翻书不百度) 输入 问题一:从文件abc.txt中读取数据到字节数组并打印出来. 分析:如果读取数据,首先第一个问题,数据有多少?如果数据量不确定,如果确定字节数组大小 ...
- alfred3配置
搜索功能配置 github https://github.com/search?utf8=%E2%9C%93&q={query} github'{query}' g baidu http:// ...
- Breathe me
Help, I have done it again 帮帮我,我又做错了. I have been here many times before 哪怕这已经不是一两次了. Hurt myself ag ...
- 【转】在Linux下安装python3
原文链接:http://www.cnblogs.com/feng18/p/5854912.html 1.linux下安装python3 a. 准备编译环境(环境如果不对的话,可能遇到各种问题,比如wg ...
- linux下tomcat服务器的相关命令
一:Linux下tomcat服务的启动.关闭与错误跟踪,使用PuTTy远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务:切换到tomcat主目录下的bin目录(cd usr/loca ...
- Ubuntu18.04或者Deepin15.8 部署Django项目
一.首先先安装nginx静态服务 1.安装gcc g++的依赖库sudo apt-get install build-essential && sudo apt-get install ...
- Docker学习笔记-Docker for Windows 安装
前言: 环境:windows10专业版 64位 正文: 官方下载地址:https://hub.docker.com/editions/community/docker-ce-desktop-windo ...