122.买卖股票的最佳时机II
class Solution {
public int maxProfit(int[] prices) {
int result = 0;
for(int i = 1;i<prices.length;i++){
result += Math.max(prices[i] - prices[i-1],0);
}
return result;
}
}
- 跳跃游戏
class Solution {
public boolean canJump(int[] nums) {
if (nums.length == 1) {
return true;
}
int cover = 0;
for(int i = 0; i<= cover ;i++){
cover = Math.max(i + nums[i],cover);
if(cover >= nums.length -1) return true;
}
return false;
}
}
45.跳跃游戏II
class Solution {
public int jump(int[] nums) {
if(nums.length == 1) return 0;
int cur = 0;
int next = 0;
int result = 0;
for(int i = 0;i<nums.length;i++){
if(cur >= nums.length-1) return result;
//下一步跳的最远的距离
next = Math.max(nums[i] +i ,next);
if(i == cur ){
result ++;
cur = next;
}
//if(cur >= nums.length -1) break;
}
return result;
}
}