Question
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、双指针法

【复杂度】

【思路】

【代码】

`public class Solution {    public int jump(int[] nums) {        //require        if(nums==null)            return 0;        int size=nums.length;        if(size<=1)            return 0;        //invariant        int step=0,preHigh=0,high=0,low=0;        while(high<size-1){            step++;            preHigh=high;            for(int i=low;i<=preHigh;i++)                high=Math.max(i+nums[i],high);            low=preHigh+1;        }        //ensure        return step;    }}`

2、BFS

【复杂度】

【思路】

【代码】

`public class Solution {    public int jump(int[] nums) {        //require        if(nums==null)            return 0;        int size=nums.length;        if(size<=1)            return 0;        //initialize queue        Set<Integer> set=new HashSet<Integer>();        Queue<Element> q=new LinkedList<Element>();        q.offer(new Element(0,0));        set.add(0);        //invariant        while(!q.isEmpty()){            Element e=q.poll();            if(e.index+nums[e.index]>=size-1)                return e.step+1;            for(int i=1;i<=nums[e.index]&&i+e.index<size;i++){                if(!set.contains(e.index+i)){                    q.offer(new Element(e.step+1,e.index+i));                    set.add(e.index+i);                }            }        }        return 0;    }    class Element{        int step=-1;        int index=-1;        public Element(int step,int index){            this.step=step;            this.index=index;        }    }}`

参考

​​[Leetcode] Jump Game 跳跃游戏​​