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 = ...
随机推荐
- kettle每天自动发送邮件总结_20161128
kettle作为java开发的工具,很多功能在目前工作中还用不到,原来它也是支持java代码的,现在用到的也就是它从服务器导数到数据库,然后再进行数据处理的功能. 如何快速学会使用kettle发送邮件 ...
- 结合Mysql和kettle邮件发送日常报表_20161001
十一假期 参加婚礼 稍晚点发博 整体流程步骤是: 写SQL-导出到excel设定excel模板调整格式-设置kettle转换--设置kettle邮件作业--完成 第一.写SQL 保持最近12个周的数据 ...
- 「LuoguP4180」 【模板】严格次小生成树[BJWC2010](倍增 LCA Kruscal
题目描述 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正当小C洋洋得意之时,小P又来泼小C冷水了.小P说,让小C求出一个无向图的次小生成树,而且这个次小生成树还得 ...
- JVM(一)虚拟机内存划分
Java内存区域 线程私有数据区域:虚拟机栈,本地方法栈,程序计数器 线程共享数据区域:方法区,堆 程序计数器:当前线程所执行的字节码的行号指示器,JVM通过这个字节码解释器改变计数器的值,以选择下一 ...
- Python 静态方法和类方法的区别
python staticmethod and classmethod Though classmethod and staticmethod are quite similar, there’s a ...
- 树——平衡二叉树插入和查找的JAVA实现
package com.tomsnail.data.tree; /** * AVL二叉平衡树 * @author tomsnail * @date 2015年3月30日 下午4:35:50 */ pu ...
- XAML 编码规范 (思考)
1.尽量和Blend统一 2.兄弟元素之间需要空行 4.父子元素之间不需要空格 3.每行尽量单个属性 5.Grid的Row和Column定义不需要空行 6.Style里的Setter中不需要单行一个属 ...
- Python3解leetcode Linked List Cycle
问题描述: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given link ...
- nagios监控windows配置
1.下载并安装windows插件 http://sourceforge.net/projects/nscplus/NSCP-0.4.1.73-x64.msi2.windows端配置 nsclient. ...
- printf函数指向串口的方法
简单地说:想在mdk 中用printf,需要同时重定义fputc函数和避免使用semihosting(半主机模式),标准库函数的默认输出设备是显示器,要实现在串口或LCD输出,必须重定义标准库函数里调 ...