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 ...
随机推荐
- com.Goods.ForEach
com.Goods.ForEach(g => { g.TransactionPrice = getUnitPriceByProductId(g.ProductID); g.ExpressMone ...
- PID
http://blog.gkong.com/liaochangchu_117560.ashx
- UIAlertControl swift
let alertController = UIAlertController(title: "开始!", message: "游戏就要开始,你准备好了吗?", ...
- RSA算法详解
1.RSA加密算法是最常用的非对称加密算法 2.RSARSA以它的三个发明者Ron Rivest, Adi Shamir, Leonard Adleman的名字首字母命名, 3.目前学术界无法证明RS ...
- C++类中的this指针的作用
1.我们知道C++的类成员函数中,默认都隐含了一个this指针,标识调用该成员函数的对象 2.为什么需要有一个this指针呢?C++设计这个机制的初衷是什么呢? 我们知道,普通的C++类,其成员函数是 ...
- jQuery Dialog弹出层对话框插件
Dialog.js的相关注释已经添加,可以按照注释,进行相关样式的修改,适用于自定义的各个系统! dialog.js /** * jQuery的Dialog插件. * * @param object ...
- Chapter 3 Discovering Classes and Object
Chatper 3 Discovering Classes and Object Exercises: 1.What is a class? A class is a template for man ...
- 《JavaScript高级程序设计》第3章 基本概念
3.4 数据类型 3.4.1 typeof操作符 var message = 'some string'; console.log(typeof message); // 'string' conso ...
- iTunes Connect TERMS OF SERVICE
iTunes Connect TERMS OF SERVICE THESE TERMS OF SERVICE CONSTITUTE A LEGAL AGREEMENT BETWEEN YOU AND ...
- PowerDesigner(八)-面向对象模型(用例图,序列图,类图,生成Java源代码及Java源代码生成类图)(转)
面向对象模型 面向对象模型是利用UML(统一建模语言)的图形来描述系统结构的模型,它从不同角度实现系统的工作状态.这些图形有助于用户,管理人员,系统分析人员,开发人员,测试人员和其他人员之间进行信息交 ...