055 Jump Game 跳跃游戏
给定一个非负整数数组,您最初位于数组的第一个索引处。
数组中的每个元素表示您在该位置的最大跳跃长度。
确定是否能够到达最后一个索引。
示例:
A = [2,3,1,1,4],返回 true。
A = [3,2,1,0,4],返回 false。
详见:https://leetcode.com/problems/jump-game/description/
Java实现:
方法一:
- class Solution {
- public boolean canJump(int[] nums) {
- int n=nums.length;
- // maxJump是维护的当前能跳到的最大位置
- int maxJump=0;
- for(int i=0;i<n;++i){
- // i>maxJump表示无法到达i的位置,失败
- // maxJump >= (n - 1),此时的距离已经足够到达终点,成功
- if(i>maxJump||maxJump>=(n-1)){
- break;
- }
- // nums[i]+i当前跳最远距离 maxJump为i之前跳最远距离
- maxJump=maxJump>(i+nums[i])?maxJump:(i+nums[i]);
- }
- return maxJump>=(n-1);
- }
- }
方法二:
- class Solution {
- public boolean canJump(int[] nums) {
- int n=nums.length;
- // dp[i]表示当前跳跃的最大距离
- int[] dp=new int[n];
- dp[0]=nums[0];
- // i表示当前距离,也是下标
- for(int i=1;i<n;++i){
- // i点可达
- if(i<=dp[i-1]){
- dp[i]=dp[i-1]>(nums[i]+i)?dp[i-1]:(nums[i]+i);
- }else{
- dp[i]=dp[i-1];
- }
- }
- return dp[n-1]>=(n-1);
- }
- }
参考:https://blog.csdn.net/mine_song/article/details/69791029
055 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(跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode55. Jump Game跳跃游戏
给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...
- [LeetCode] 45. 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! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 跳跃游戏 12 · Jump Game 12
跳跃游戏 1 [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: 由于要用itera ...
- lintcode: 跳跃游戏 II
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = ...
随机推荐
- 剑指offer24:判断一个二叉树的后序遍历序列是否为二叉搜索树的后序遍历序列
public static boolean isBSTSequence(int[] s,int l, int r) { if (s == null || r <= 0) return false ...
- 01PS基础
通道:记录颜色信息 alpha通道:主要用来记录选取 画笔颜色模式:会保留纹理,不要直接在原图上画,可以新建一个层,然后变成颜色模式 调整色阶(ctrl + l)的三种方式:1.输入:修改前 > ...
- docker安装与操作
准备和安装 1.到这个路径下下载docker engine: https://get.docker.com/rpm/1.7.1/centos-7/RPMS/x86_64/docker-engine-1 ...
- poj 2069 Super Star——模拟退火(收敛)
题目:http://poj.org/problem?id=2069 不是随机走,而是每次向最远的点逼近.而且也不是向该点逼近随意值,而是按那个比例:这样就总是接受,但答案还是要取min更新. 不知那个 ...
- POJ2182:Lost Cows
浅谈线段树和树状数组:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:http://poj.org/problem?id=2182 线段树,倒着确 ...
- 用Entity Framework 来创建MySql数据库和表结构
1. 创建VS工程 2.添加新项, 选中ADO.Net Entity Data Model模板 3.填入Host及数据库名字, 如果没有此数据库, 会提示创建 4.添加edmx后, 右击选择属性,配置 ...
- ORACLE常用数据库字段类型
ORACLE常用数据库字段类型 常用的数据库字段类型如下: 字段类型 中文说明 限制条件 其它说明 CHAR 固定长度字符串 最大长度2000 bytes VARCHAR2 可变长度 ...
- JavaScript总结(1)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- java 通过System.getProperties()获取系统参数
转自:https://www.cnblogs.com/ksuifeng/archive/2010/09/25/1834416.html 1.java的System.getProperty()方法可以获 ...
- java类加载器(转)
类加载器是 Java 语言的一个创新,也是 Java 语言流行的重要原因之一.它使得 Java 类可以被动态加载到 Java 虚拟机中并执行.类加载器从 JDK 1.0 就出现了,最初是为了满足 Ja ...