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 2.7.x 和 3.x 版本的语法区别
<__future__模块> Python 3.x引入了一些与Python 2不兼容的关键字和特性,在Python 2中,可以通过内置的__future__模块导入这些新内容.如果你希望在 ...
- FastReport.Net使用:[2]添加MSSQL数据源一
如何使用MSSQL表作为数据源 1.点击FastReport设计器中Data->Add Data Source菜单项,打开数据源添加向导. 2.添加新的数据连接. 点击 New connecti ...
- 【BZOJ 1449】 1449: [JSOI2009]球队收益 (最小费用流)
1449: [JSOI2009]球队收益 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 841 Solved: 483 Description Inpu ...
- BZOJ 1030 [JSOI2007]文本生成器(AC自动机)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1030 [题目大意] 求出包含任意一个给定串的串数量 [题解] 我们求出不包含任意一个给 ...
- poj 1703 并查集
题意:在这个城市里有两个黑帮团伙,现在给出N个人,问任意两个人他们是否在同一个团伙 输入D x y代表x于y不在一个团伙里 输入A x y要输出x与y是否在同一团伙或者不确定他们在同一个团伙里 链接: ...
- 2015 UESTC 数据结构专题H题 秋实大哥打游戏 带权并查集
秋实大哥打游戏 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...
- hihocoder 162周 1323 : 回文字符串
hihocoder1323 : 回文字符串(162周) 题目链接 思路: dp; ac代码: #include<iostream> #include<cstdio> #incl ...
- 面试&笔试---c语言之字符串处理
1.字串拷贝库函数strcpy 函数介绍: 原型声明:extern char *strcpy(char *dest,const char *src); 头文件:string.h 功能:把从src地址开 ...
- High-current supply uses standard three-terminal regulator
Voltage-regulator design for high output currents can be a critical and difficult task. Although vol ...
- 一、 Log4E插件下载
下载地址:http://log4e.jayefem.de/content/view/3/2/ 二.安装Log4E插件 将下载下来的压缩包解压缩,如下图所示: 解压缩生成的[de.jayefem.log ...