jump game

来源:互联网 发布:cloverv3.3是什么软件 编辑:程序博客网 时间:2024/05/23 11:29

中等 跳跃游戏

40%
通过

给出一个非负整数数组,你最初定位在数组的第一个位置。   

数组中的每个元素代表你在那个位置可以跳跃的最大长度。    

判断你是否能到达数组的最后一个位置。

您在真实的面试中是否遇到过这个题? 
哪家公司问你的这个题? 
感谢您的反馈
样例

A = [2,3,1,1,4],返回 true.

A = [3,2,1,0,4],返回 false.

动规解法:
public class Solution {    /**     * @param A: A list of integers     * @return: The boolean answer     */    public boolean canJump(int[] A) {        // wirte your code here        boolean[] can = new boolean[A.length];        //can[i] 表示能不能从前面跳到第i个位置        //can[0] 是初始位置,永远能跳到        can[0] = true;        //遍历数组        for (int i = 1; i < A.length; i++) {            //遍历数组,看角标为i的元素是否能到达            /**             * A = [2,3,1,1,4]             * i = 1 , j = 0, can[0] = true && 0+A[0] >=1 ==> can[1] = true;             *              * i = 2 , j = 0, can[0] = true && 0+A[0] >=2 ==> can[2] = true; break;             *                      * i = 3 , j = 0, can[0] = true && 0+A[0] < 2;             *         j = 1, can[1] = true && 1+A[1] >=3 ==> can[3] = true; break;             *                      * i = 4 , j = 0, can[0] = true && 0+A[0] < 2;             *         j = 1, can[1] = true && 1+A[1] >=4 ==> can[4] = true; break;             *              * A = [3,2,1,0,4]             * i = 1 , j = 0, can[0] = true && 0+A[0] >=1 ==> can[1] = true;             *              * i = 2 , j = 0, can[0] = true && 0+A[0] >=2 ==> can[2] = true; break;             *                      * i = 3 , j = 0, can[0] = true && 0+A[0] >=3 ==> can[3] = true; break;             *                      * i = 4 , j = 0, can[0] = true && 0+A[0] < 4;             *         j = 1, can[1] = true && 1+A[1] < 4;             *         j = 2, can[2] = true && 2+A[2] < 4;             *         j = 3, can[3] = ture && 3+A[3] < 4;             * ===> return false;             *              */             //看当前的i能否通过之前的位置到达            for (int j = 0; j < i; j++) {                if (can[j] && j + A[j] >= i) {                    can[i] = true;                    break;                }            }        }                return can[A.length - 1];    }}


贪心解法:
public class Solution {    /**     * @param A: A list of integers     * @return: The boolean answer     */    public boolean canJump(int[] A) {        // wirte your code here        if(A.length == 0 || A == null){            return false;        }        int farthest = A[0];        for (int i = 1; i < A.length; i++) {            //i<=farthest保证是在当前farthest的范围之内            //A[i] + i >= farthest, 在当前farthest的范围内看能不能继续往后跳            if (i <= farthest && A[i] + i >= farthest) {                farthest = A[i] + i;            }        }        //当遍历完了整个数组,看能不能跳到最后一位        return farthest >= A.length - 1;    }}


上面的方法leetcode超时了,下面是贪心,没超时:

public class Solution {    public boolean canJump(int[] nums) {        if(nums == null || nums.length == 0){            return false;        }        // reach 代表能到的最远距离        int reach = 0;        // i>reach 代表reach到不了i的位置,则循环结束        for(int i = 0; i<=reach && i<nums.length; i++){            reach = Math.max(reach,nums[i]+i);        }        //看reach能否到达末尾        if(reach < nums.length-1){            return false;        }        return true;    }}





0 0