*LeetCode-Jump Game II

不是dp!!!是bfs就可以 并且不需要queue 只需要keep一个最远的boundary然后每次一层就是这一步可以到达的最远的位置 然后一层一层看能否push boundary

public class Solution {
    public int jump(int[] nums) {
        if ( nums == null || nums.length <= 1)
            return 0;
        int length = nums.length;
        int bound = 0;
        int level = 0;
        int start = 0;
        while ( bound >= start ){
            level ++;
            int oldBound = bound;
            for ( int i = start; i <= oldBound; i ++ ){
                bound = Math.max ( bound, i + nums[ i ] );
                if ( bound >= length - 1 )
                    return level;
            }
            start = oldBound + 1;
        }
        return 0;
    }
}


你可能感兴趣的:(*LeetCode-Jump Game II)