122.买卖股票的最佳时机II

思路:

代码:

class Solution {
    public int maxProfit(int[] prices) {
        if (prices.length == 0 || prices.length == 1) {
            return 0;
        }
        int maxProfit = 0;
        for (int i = 1; i < prices.length; i++) {
            int sub = prices[i] - prices[i-1];
            if (sub>0) {
                maxProfit+=sub;
            }
        }
        return maxProfit;
    }
}

55. 跳跃游戏

思路:

代码:

class Solution {
    public boolean canJump(int[] nums) {
        if (nums.length == 0) {
            return false;
        }
        int maxJump = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i<=maxJump) {
                maxJump = Math.max(maxJump, i+nums[i]);
                continue;
            }
            return false;
        }
        return true;
    }
}

45. 跳跃游戏II

思路:

代码:

class Solution {
    public int jump(int[] nums) {
        int targetNum = nums.length-1;
        int jumpTime = 0;
        while (targetNum>0) {
            for (int i = 0; i < nums.length; i++) {
                if (i+nums[i]>=targetNum) {
                    targetNum=i;
                    jumpTime++;
                    break;
                }
            }
        }
        return jumpTime;
    }
}
04-23 16:44