[刷题]Jump Game

来源:互联网 发布:php 获取session数据 编辑:程序博客网 时间:2024/05/16 11:40

[LintCode]Jump Game

public class Solution {    /**     * @param A: A list of integers     * @return: The boolean answer     */    public boolean canJump(int[] A) {        // 2015-05-15        if (A == null || A.length == 0) {            return false;        }                int n = A.length;        boolean[] can = new boolean[n];                can[0] = true;        for (int i = 1; i < n; i++) {            can[i] = false;            for (int j = 0; j < i; j++) {                if (can[j] == true && j + A[j] >= i) {                    can[i] = true;                    break;                }            }        }                return can[n - 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 == null || A.length == 0) {            return false;        }                // 默认初始化为false        boolean[] can = new boolean[A.length];        can[0] = true;        for (int i = 0; i < A.length; i++) {            if (!can[i]) {                continue;            }            for (int j = i + 1; j <= i + A[i]; j++) {                if (j < A.length)                can[j] = true;            }        }        return can[A.length - 1];    }}


0 0
原创粉丝点击