045 Jump Game II 跳跃游戏 II
给定一个非负整数数组,你最初位于数组的首位。
数组中的每个元素表示你在该位置的最大跳跃长度。
你的目标是用最小跳跃次数到达最后一个索引。
例如:
给定一个数组 A = [2,3,1,1,4]
跳到最后一个索引的最小跳跃数是 2。(从索引 0 跳到 1 跳1步,然后跳3步到最后一个索引。)
注意:
假设你总是可以到达最后一个索引位置。
详见:https://leetcode.com/problems/jump-game-ii/description/
Java实现:
curReach是维护的当前能跳到的最大位置,maxReach是指从之前的点能到达到得最远位置。
当i 大于之前点能跳到的最大位置时,就需要跳一步,并把curReach更新为maxReach。
最后返回若是curReach能到最后一个元素,就返回step, 若是到不了,就说明根本走不到最后一步,返回0。
参考:http://www.cnblogs.com/Dylan-Java-NYC/p/4834079.html
class Solution {
public int jump(int[] nums) {
int n=nums.length;
int step=0;
int curReach=0;
int maxReach=0;
for(int i=0;i<n;++i){
if(curReach<i){
++step;
curReach=maxReach;
}
maxReach=maxReach>(nums[i]+i)?maxReach:(nums[i]+i);
}
return maxReach<n-1?0:step;
}
}
C++实现:
class Solution {
public:
int jump(vector<int>& nums) {
int n=nums.size();
int res=0;
int curRch=0;
int maxRch=0;
for(int i=0;i<n;++i)
{
if(curRch<i)
{
++res;
curRch=maxRch;
}
maxRch=max(maxRch,nums[i]+i);
}
return res;
}
};
参考:http://www.cnblogs.com/ganganloveu/p/3761715.html
045 Jump Game II 跳跃游戏 II的更多相关文章
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- lintcode: 跳跃游戏 II
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = ...
- Leetcode力扣45题 跳跃游戏 II
原题目: 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...
- LeetCode 45. 跳跃游戏 II | Python
45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...
- Java实现 LeetCode 45 跳跃游戏 II(二)
45. 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- [leetcode] 45. 跳跃游戏 II(Java)(动态规划)
45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...
- leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II
55. 跳跃游戏 问题描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...
- LeetCode(45): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
随机推荐
- 联系E-R:学生选课系统
- hadoop集群的安装
Hadoop集群安装 1.配置JDK环境和设置主机名,本地解析 JDK环境教程: http://www.cnblogs.com/wangweiwen/p/6104189.html 本地解析: vim ...
- ubuntu的root权限设置
Linux操作系统有root权限用户和普通权限用户两种模式. 在执行一些需要权限才能执行的任务时,我们需要转化到root权限用户条件下才能执行. 1.普通用户权限转临时root权限: Linux中,通 ...
- PS 滤镜— — sparkle 效果
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- PS 图像调整— — gain and bias
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); Image=im ...
- BZOJ_3529_[Sdoi2014]数表_莫比乌斯反演+树状数组
Description 有一张 n×m 的数表,其第 i 行第 j 列(1 <= i <= n, 1 <= j <= m)的数值为 能同时整除 i 和 j 的所有自然数之和.给 ...
- ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)
Description There is a special number sequence which has n+1 integers. For each number in sequence, ...
- 【Lintcode】094.Binary Tree Maximum Path Sum
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- opencv报错 error: (-215) size.width>0 && size.height>0 in function cv::imshow
使用opencv读取摄像头并且显示事出现此问题: 后来发现是图像为空时的错误,加入: if(!frame.empty()) imshow("video",frame); 完整的代码 ...