leetcode 随笔 Jump Game II

3人阅读 评论(0) 收藏 举报
分类:

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.

Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: 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.

这个题最大的难点对我来说是理解题意。。。。

题意是这样的,就是给你一个数组,你开始在0的位置,然后你跳到哪个位置,哪个位置上的数表示你下一步最多能跳多少格。

比如[2,3,1,1,4],你开始在位置0,位置0上的数字是2,你最多可以跳两格,也就是说可以跳到位置1和位置2,位置1位置2上的数字分别是3和1,如果你跳到位置1,那么下一步你最多跳3步就直接到终点了。

问题是求最短的跳数。

这个题基本上看懂了就能看出来其实跳数和位置之间是有包含关系的,比如位置1上的3,包含了,位置2上的1,所以下一步要跳也得跳位置1,然后每次求最佳的跳位即可。

runtime 13ms

class Solution {
public:
	int jump(vector<int>& nums) {
		if (nums.empty()||nums[0]==0||nums.size()==1) return 0;
		int size = nums.size();
		int count = 0;
		int index = 0;
		if (nums[0] >= size - 1)return 1;
		while (index != size - 1)
		{
			count++;			
			if (nums[index] + index >= size - 1) return count;
			int record = nums[index + 1] + index + 1, next_index = index + 1;
			for (int i = 0; i < nums[index]-1; i++)
			{
				if (index + i + 2 + nums[index + i + 2] >= record)
				{
					record = index + i + 2 + nums[index + i + 2];
					next_index = index + i + 2;
				}
			}
			if (next_index == record) return 0;
			index = next_index;
		}
		return count;
	}
};

查看评论

LeetCode_Jump Game&&Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the arra...
  • loveRooney
  • loveRooney
  • 2014-08-09 12:46:38
  • 9840

【LeetCode】Jump Game II 解题报告

【题目】 Given an array of non-negative integers, you are initially positioned at the first index o...
  • ljiabin
  • ljiabin
  • 2014-12-09 17:17:02
  • 3795

Jump Game II -- LeetCode

原题链接: http://oj.leetcode.com/problems/jump-game-ii/  这道题是Jump Game的扩展,区别是这道题不仅要看能不能到达终点,而且要求到达终点的最少...
  • linhuanmars
  • linhuanmars
  • 2014-03-20 23:25:59
  • 15131

LeetCode --- 45. Jump Game II

题目链接:Jump Game II Given an array of non-negative integers, you are initially positioned at the firs...
  • makuiyu
  • makuiyu
  • 2015-02-10 08:59:21
  • 1168

[leetcode]45. Jump Game II 跳棋游戏2 C++/PYTHON实现【hard难度】

题目 Given an array of non-negative integers, you are initially positioned at the first index of the...
  • zl87758539
  • zl87758539
  • 2016-06-17 10:15:08
  • 452

Jump Game II (最小步数到达终点,贪心) 【leetcode】

题目: Given an array of non-negative integers, you are initially positioned at the first index of the...
  • zhang9801050
  • zhang9801050
  • 2013-09-20 19:33:17
  • 13064

leetcode解题之45. Jump Game II&55. Jump Game java (跳跃游戏)

leetcode解题之45. Jump Game II&55. Jump Game java (跳跃游戏)。动态规划,贪心。
  • mine_song
  • mine_song
  • 2017-04-09 01:09:19
  • 1113

【一天一道LeetCode】#45. Jump Game II

一天一道LeetCode系列(一)题目 Given an array of non-negative integers, you are initially positioned at the f...
  • terence1212
  • terence1212
  • 2016-05-09 15:10:23
  • 692

[LeetCode]Jump Game II(贪心&&DP!!!!!)

Given an array of non-negative integers, you are initially positioned at the first index of the a...
  • whzyb1991
  • whzyb1991
  • 2015-07-18 13:27:47
  • 477

【LeetCode】Jump Game 解题报告

【题目】 Given an array of non-negative integers, you are initially positioned at the first index of t...
  • ljiabin
  • ljiabin
  • 2014-12-09 09:59:53
  • 6300
    个人资料
    等级:
    访问量: 218
    积分: 233
    排名: 31万+
    文章分类
    文章存档