提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、力扣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;
}
}
二、力扣55. 跳跃游戏
class Solution {
public boolean canJump(int[] nums) {
if(nums.length == 1){
return true;
}
int cover = 0;
for(int i = 0; i <= cover && i < nums.length; i ++){
cover = Math.max(cover, i + nums[i]);
if(cover >= nums.length -1){
return true;
}
}
return false;
}
}
三、力扣45. 跳跃游戏 II
class Solution {
public int jump(int[] nums) {
if(nums == null || nums.length == 0 || nums.length == 1){
return 0;
}
int count = 0;
int curDistance = 0;
int MaxDistance = 0;
for(int i = 0; i < nums.length; i ++){
MaxDistance = Math.max(MaxDistance, i + nums[i]);
if(MaxDistance >= nums.length - 1){
count ++;
break;
}
if(i == curDistance){
count ++;
curDistance = MaxDistance;
}
}
return count;
}
}
883

被折叠的 条评论
为什么被折叠?



