lintcode: 跳跃游戏 II
跳跃游戏 II
给出一个非负整数数组,你最初定位在数组的第一个位置。
数组中的每个元素代表你在那个位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
样例
给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃次数是2(从数组下标0跳一步到数组下标1,然后跳3步到数组的最后一个位置,一共跳跃2次)
解题
终于自己还是没有解决出来
参考链接 理解不透
public class Solution {
/**
* @param A: A list of lists of integers
* @return: An integer
*/
public int jump(int[] A) {
int target = A.length-1;
int cnt = 0;
while(target > 0) {
for(int i=0; i<target; i++) {
if(i+A[i] >= target) {
target = i;
cnt++;
break;
}
}
}
return cnt;
}
}
这个好理解
public class Solution {
/**
* @param A: A list of lists of integers
* @return: An integer
*/
public int jump(int[] nums)
{
int ret = 0;
int curMax = 0;
int curRch = 0;
for(int i = 0; i < nums.length; i ++) // 一个一个的扫描
{
if(curRch < i)// 跳不到 i位置,选取前面可以跳的最远 位置
{
ret ++;
curRch = curMax;
}
curMax = Math.max(curMax, nums[i]+i);// 能够跳到最远的 那个位置
}
return ret;
}
}
Python
class Solution:
# @param A, a list of integers
# @return an integer
def jump(self, A):
# write your code here
if A == None or len(A)<= 1:
return 1
MaxJump = A[0]
subJump = A[0]
count = 1
for i in range(1,len(A)):
if subJump <i:
count+=1;
subJump = MaxJump
MaxJump = max(MaxJump , A[i] + i) return count
lintcode: 跳跃游戏 II的更多相关文章
- 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, ...
- lintcode : 跳跃游戏
跳跃游戏 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 判断你是否能到达数组的最后一个位置. 样例 A = [2,3,1,1,4],返回 ...
- 【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): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- vsftpd配置文件说明
(1)常用选项: chroot_local_user=YES #限制所有的用户均不能切换到其他目录 allow_writeable_chroot=YES #允许根目录可写 FTP的工作模式有两种,一种 ...
- C++ STL中Map的按Key排序和按Value排序
map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...
- Teamwork——Week4 团队分工和预估项目时间
由于我们给每个组员预估的每天用在该团队项目的时间为2h左右,因此我们的时间计算也已2h为基数.下面就是我们的团队分工和预估项目时间. 任务编号 实现人员 任务详细描述 预估时间 任务0 全体组员 看学 ...
- 团队作业week3 团队成员规划
划分角色:PM:黄剑锟 Dev:顾泽鹏 周辰光 孙时 Test:龚少波 赵骞 彭佟 团队贡献分的分配方法:设定两个考察方面,分别为工作量和任务难度,每个人的个人单次得分为该次任务的 工作量(最高10分 ...
- android apk 自我保护技术-加密apk
经过了忙碌的一周终于有时间静下来写点东西了,我们继续介绍android apk防止反编译技术的另一种方法.前两篇我们讲了加壳技术(http://my.oschina.net/u/2323218/blo ...
- 【Dancing Link专题】解题报告
DLX用于优化精确覆盖问题,由于普通的DFS暴力搜索会超时,DLX是一个很强有力的优化手段,其实DLX的原理很简单,就是利用十字链表的快速删除和恢复特点,在DFS时删除一些行和列以减小查找规模,使得搜 ...
- poj 1422 Air Raid 最少路径覆盖
题目链接:http://poj.org/problem?id=1422 Consider a town where all the streets are one-way and each stree ...
- zip&rar格式压缩包的解压缩
实际工作中例子: 首先需要引入两个jar包: 注意:支持压缩软件4.20及以上版本 (1)压缩包的解压: (2)压缩包的压缩打包 zip格式:
- HDU1048The Hardest Problem Ever
The Hardest Problem Ever Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- Android 4.4 KitKat, the browser and the Chrome WebView
Having V8 as the JavaScript engine for the new web view, the JavaScript performance if much better, ...