122.买卖股票的最佳时机II
题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
解题思路:只算正利润
java:
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. 跳跃游戏
题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
解题思路:每次取最大跳跃范围
java:
class Solution {
public boolean canJump(int[] nums) {
if (nums.length == 1) {
return true;
}
int coverRange = 0;
for (int i = 0; i <= coverRange; i++) {
coverRange = Math.max(coverRange, i + nums[i]);
if (coverRange >= nums.length - 1) {
return true;
}
}
return false;
}
}
45.跳跃游戏II
题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
解题思路:走到最大覆盖区域,count计数一次
java:
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){
curDistance = maxDistance;
count++;
}
}
return count;
}
}