leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
思路:本题是典型的贪心算法。题目难度上也不算难,贪心策略是每步前进的地方是下一步能达到的地方最远。
具体代码例如以下:
public class Solution {
public int jump(int[] nums) {
/**
* 本题用贪心法求解,
* 贪心策略是在每一步可走步长内。走最大前进的步数
*/
if(nums.length <= 1){
return 0;
}
int index,max = 0;
int step = 0,i= 0;
while(i < nums.length){
//假设能直接一步走到最后,直接步数+1结束
if(i + nums[i] >= nums.length - 1){
step++;
break;
}
max = 0;//每次都要初始化
index = i+1;//记录索引。最少前进1步
for(int j = i+1; j-i <= nums[i];j++){//搜索最大步长内行走最远的那步
if(max < nums[j] + j-i){
max = nums[j] + j-i;//记录最大值
index = j;//记录最大值索引
}
}
i = index;//直接走到能走最远的那步
step++;//步长+1
}
return step;
}
}
leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法的更多相关文章
- [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. Jump game II ☆☆☆☆☆(跳跃游戏 2)
https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode 113. Path Sum II (路径和) 解题思路和方法
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【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
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- Python协程(下)
停止子线程 如果一切正常,那么上面的例子很完美.可是,需要停止程序,直接ctrl+c,会抛出KeyboardInterrupt错误,我们修改一下主循环: try: while True: task = ...
- CodeForces - 1016D Vasya And The Matrix
题面在这里! 很明显二进制每一位都是无关的,所以可以先把原问题简化:给矩阵中的每个位置填入0/1,使得特定行/列有奇数个1,其他行/列有偶数个1. 一个比较好想的方法是对行和列 列出 n+m 个异或方 ...
- BZOJ1509 [NOI2003]逃学的小孩 树型DP
题目: 分析: 首先明确我们是要求 min(dist[C][A],dist[C][B])+dist[A][B]. 我们把C当成树根,第一我们可以发现min里面取dist[C][A]或者dist[C][ ...
- Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法
BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 1044 Solved: 322[Submit][ ...
- 内功心法 -- java.util.LinkedList<E> (5)
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------下文主要对java.util ...
- Codeforces Round #245 (Div. 2) B. Balls Game 并查集
B. Balls Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...
- uva 6959 Judging hash
Judging Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProb ...
- 使用Keras实现机器翻译(英语—>法语)
import numpy as np from keras.models import Model from keras.models import load_model from keras.lay ...
- JS 中对变量类型的判断
总结:1. 一般简单的使用 typeof 或 instanceof 检测(这两种检测的不完全准确) 2. 完全准确的使用 原生js中的 Object.prototype.toStri ...
- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...