45.jump游戏第二弹
来源:互联网 发布:软件统计表 编辑:程序博客网 时间:2024/06/05 02:33
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.)
测试代码:
class Solution { bool find_prior(vector<int> nums,vector<int> &jump,int pos) { if(nums[pos]+pos>=nums.size()-1) { jump[nums.size()-1] = jump[pos] + 1; return true; } for(int i=1;i<=nums[pos];i++) { jump[pos+i] = min(jump[pos+i],jump[pos]+1); } return false; } public: int jump(vector<int>& nums) { vector<int> jump(nums.size(),nums.size()); jump[0] = 0; for(int j=0;j<nums.size()-1;j++) { if(j>=1&&nums[j]<=nums[j-1]-1) continue; if(find_prior(nums,jump,j)) break; } return jump[nums.size()-1]; }};
性能:
参考答案:
class Solution {public: int jump(vector<int>& nums) { int n = nums.size(); int reach = 0; int last_reach = 0; int step = 0; for(int i=0; i <= reach && i < n; ++i) { if(i > last_reach) { ++step; last_reach = reach; } reach = max(nums[i] + i, reach); } if( reach < n-1) return -1; return step; }};
性能:
阅读全文
1 0
- 45.jump游戏第二弹
- 55.jump游戏
- leetcode解题之45. Jump Game II&55. Jump Game java (跳跃游戏)
- Leetcode #45. Jump Game II 跳跃游戏2 解题报告
- LeetCode--------45. Jump Game II(跳跃游戏--最短路径)
- 45. Jump Game II(跳跃游戏II)
- 45. Jump Game II(跳跃游戏2)
- 数组跳跃游戏 Jump Game
- LintCode Jump Game 跳跃游戏
- [Lintcode]Jump Game跳跃游戏
- [LeetCode] Jump Game 跳跃游戏
- LintCode Jump Game 跳跃游戏
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
- [leetcode]45. Jump Game II 跳棋游戏2 C++/PYTHON实现【hard难度】
- LeetCode Jump Game 前跳游戏
- LeetCode | Jump Game(跳远游戏)
- [LintCode] 跳跃游戏II Jump Game II
- 55/45 Jump Game 跳跃游戏
- javascript模块化编程
- ASP.NET MVC – 视图
- 微信返回键的监控
- 【洛谷】P1002-过河卒
- VB.NET书籍整理,持续更新!
- 45.jump游戏第二弹
- 安卓Palette调色板
- sqlloader中关于ctl控制文件的相关参数说明:
- PCLint与SourceInsight
- tensorflow学习——tfreader格式,队列读取数据tf.train.shuffle_batch()
- Incorrect string value: '\xE8\xB0\xA2\xE8\xB0\xA2...' for column 'words' at row 1
- hdu 3496 Watch The Movie(带限制的01背包)
- Ubuntu下配置超级终端minicom
- asp.net mvc 全局权限过滤器及继成权限方法