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 = ...
随机推荐
- Java_Time_01_获取当前时间
1. Date SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// ...
- linux 多线程编程-读写者问题
#include <cstdio> #include <pthread.h> #include <unistd.h> ]; int i,j; pthread_rwl ...
- AtCoder AGC #3 Virtual Participation
Havana真好听qwq AB题就不写了 SB C.BBuBBBlesort! 有一个长度为$n$的数列 你每次可以用两种操作 1.交换两个相邻元素 2.交换两个隔且仅隔了一个的元素 求把数列排成有序 ...
- C++ 创建文件的方法
CString getPath(){ CTime time = CTime::GetCurrentTime(); CString t = time.Format(_T("%Y%m%d%H%M ...
- Linux MySQL5.5的安装
1.安装cmake [root@server1 src]# cd /opt/ipnms/src[root@server1 src]# tar zxvf cmake-2.8.4.tar.gz[root@ ...
- ubuntu_deb安装命令
dpkg命令常用格式如下: sudo dpkg -I iptux.deb#查看iptux.deb软件包的详细信息,包括软件名称.版本以及大小等(其中-I等价于--info) sudo dpkg -c ...
- Python中日志的格式化输出
import logging logfile = 'e:\\a.txt' # logging.basicConfig(filename=logfile,level=logging.INFO) # lo ...
- ActiveRecord 的类型初始值设定项引发异常
最近在研究ActiveRecord网上有很多贴子讲怎么用的.但自己照做就是出错. 最终定位在配置文件出错.应该是ActiveRecord有更新的原因.在国外的网站把配置复制了一份替换.问题解决了.我用 ...
- [zoj3195]Design the city(LCA)
解题关键:求树上三点间的最短距离. 解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$ //#pragma comment(linker, "/S ...
- linux命令配置IP详解
在Linux系统中,TCP/IP网络是通过若干个文本文件进行配置的,有时需要编辑这些文件来完成联网工作. vi /etc/sysconfig/network-scripts/ifcfg-eth0 :进 ...