【LeetCode】Jump Game && Jump Game II

133 篇文章 0 订阅
121 篇文章 2 订阅
1、Jump Game
Total Accepted: 17703 Total Submissions: 65204 My Submissions
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.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
【解题思路】
1、题目大意应该是,初始位置为0,在i位置可以走的最大步数为A[i],问能不能走到A.length-1位置。
2、基本思想应该是,在每个位置判断可以走得最远的位置,以最远位置为基础,依次更新,直到达到指定位置为止。

Java AC 408ms

public class Solution {
    public boolean canJump(int[] A) {
        int n = A.length;
        if(n == 0){
        	return false;
        }
        int maxReach = 0;
        int i = 0;
        while (i < n && i <= maxReach) {
			maxReach = Math.max(i + A[i], maxReach);
			i++;
			if(maxReach >= n - 1){
				return true;
			}
		}
        return false;
    }
}

Python AC 312ms

class Solution:
    # @param A, a list of integers
    # @return a boolean
    def canJump(self, A):
    	if not A:
    		return False
    	n = len(A)
    	maxReach = 0
    	i = 0
    	while i < n and i <= maxReach:
    		maxReach = max(i+A[i], maxReach)
    		i += 1
    		if maxReach >= n - 1:
    			return True
    	return False

2、Jump Game II
Total Accepted: 15240 Total Submissions: 61956 My Submissions
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.)
【解题思路】
1、题目大意应该是,初始位置为0,在i位置可以走的最大步数为A[i],问走到A.length-1位置所需的最短步数。
2、基本思想应该是,在每个位置判断可以走得最远的位置,以当前可以达到最远位置为基础,计算下一步可以达到最远位置,如果比当前最远位置远,步数就+1,循环判断直到到达到指定位置为止。

Java AC 460ms

public class Solution {
	public int jump(int[] A) {
		int n = A.length;
		if (n == 0) {
			return 0;
		}
		int lastMaxReach = 0;
		int maxReach = 0;
		int i = 0;
		int minStep = 0;
		while (i < n && i <= maxReach) {
			if (i > lastMaxReach) {
				lastMaxReach = maxReach;
				minStep++;
			}
			maxReach = Math.max(i + A[i], maxReach);
			i++;
		}
		return maxReach >= n - 1 ? minStep : 0;
	}
}

Python AC 252ms

class Solution:
    # @param A, a list of integers
    # @return an integer
    def jump(self, A):
        if not A:
            return 0
        n = len(A)
        maxReach = 0
        lastMaxReach = 0
        i = 0
        count = 0
        while i < n and i <= maxReach:
            if i > lastMaxReach:
                lastMaxReach = maxReach
                count += 1
            maxReach = max(i + A[i], maxReach)
            i += 1
        return count if maxReach >= n - 1 else 0

这个题目有点拗口,具体的分析可以参考http://obcerver.com/post/view/p/25




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值