LeetCode题目 Jump Game II
来源:互联网 发布:javs编程思想pdf百度云 编辑:程序博客网 时间:2024/05/17 16:43
题目:
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 {public: int jump(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if (n==1){ return 0; }int i=0;int jumps=0;int cur_max=0;while(i<n){cur_max=i+A[i];if (cur_max>0){jumps++;}if (cur_max>=n-1){return jumps;}int tempmax=0;for (int j=i+1;j<=cur_max;j++){ if (j+A[j]>tempmax) { tempmax=j+A[j]; i=j; }}}return jumps; }};
- LeetCode题目 Jump Game II
- LeetCode: Jump Game II
- LeetCode Jump Game II
- LeetCode: Jump Game II
- [Leetcode] Jump Game II
- [LeetCode] Jump Game II
- [Leetcode] Jump Game ii
- leetcode Jump Game II
- leetcode Jump Game II
- [LeetCode]Jump Game II
- [leetcode]Jump Game II
- LeetCode-Jump Game II
- [leetcode] Jump Game II
- LeetCode - Jump Game II
- LeetCode:Jump Game II
- LeetCode | Jump Game II
- 【Leetcode】Jump Game II
- LeetCode Jump Game II
- Oracle数据库新装连接串问题
- ISE中的Force Process Up-to-Date功能:ISE中如何在未综合实现的前提下打开ChipScope ?
- SLAB_POISON
- 广搜:Catch That Cow
- hdoj1863 畅通工程
- LeetCode题目 Jump Game II
- mtk android 编译命令
- Paip.YXSHOP易想商场功能模块说明
- 小米2012新品发布会(小米手机2),8月16日
- 广搜:Knights Move
- 距离
- 第十二天——Second Quiz
- nand flash 控制
- 深搜:Nightmare