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 ...
随机推荐
-
poj1056 (Trie入门)寻找字符串前缀
题意:给你一堆字符串,问是否满足对于任意两个字符串a.b,a不是b的前缀 字典树==前缀树==Trie树 trie入门题,只用到了insert和query操作 #include <cstdio& ...
-
css style与class之间的区别,cssclass
问题描述: 网页点击[导出]按钮后,将页面table内容另存成excel文件,却发现无法保存表格样式 分析过程: 1.table表格用class,而不是style.导出时并没有导出class定义 ...
-
xcode 工具栏中放大镜的替换的简单说明
1.如果是在打开的文档范围内: 查找: Command+ F 替换: Option+Command+F Replace All 是全部替 ...
-
C#多线程的死锁演示
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
-
vim格式化代码
在命令模式下,按键盘gg=G 命令含义: gg:到达文件头=:缩进G:直到文件尾
-
python学习之总结
迭代器: def gen(): a = 100 yield a a = a * 8 yield a yield 1000 for i in gen(): print(i) 创建一个函数,循环体,yie ...
-
SQLServer更改用户定义的数据库角色
更改用户定义的数据库角色注意事项 需具有以下一项或多项权限或成员身份才能运行此命令: 对角色具有 ALTER 权限 对数据库具有 ALTER ANY ROLE 权限 具有 db_securityadm ...
-
C#检查服务状态和启动关闭服务
WinForm 判断服务状态,显示服务名称和状态 https://blog.csdn.net/u013063880/article/details/78626200 C#获得服务,判断服务状态,启动服 ...
-
一劳永逸:域名支持通配符,ASP.NET Core中配置CORS
ASP.NET Core 内置了对 CORS 的支持,使用很简单,只需先在 Startup 的 ConfigureServices() 中添加 CORS 策略: public void Configu ...
-
你可能不知道的 10 条 SQL 技巧,涨知识了!
转自:http://mp.weixin.qq.com/s?__biz=MjM5NzM0MjcyMQ==&mid=2650076293&idx=1&sn=38f6acc759df ...