php做电商网站开题报告,行业门户网站制作,泰州整站优化,万网网站域名122.买卖股票的最佳时机II
题目链接
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/
题目描述 思路
看完视频讲解之后豁然开朗啊简直了#xff01;#xff01;#xff01; 统计后一天减去前一天#xff0c;差值为正数的#xff0c;再…122.买卖股票的最佳时机II
题目链接
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/
题目描述 思路
看完视频讲解之后豁然开朗啊简直了 统计后一天减去前一天差值为正数的再把他们加起来就是获利最大的了
class Solution {public int maxProfit(int[] prices) {int result 0;//要从 1 开始因为第 0 天刚刚买入还没有利息for (int i 1; i prices.length; i) {result Math.max(prices[i]-prices[i-1],0);}return result;}
}55. 跳跃游戏
题目链接
https://leetcode.cn/problems/jump-game/description/
题目描述 思路 class Solution {public boolean canJump(int[] nums) {if(nums.length1) return true;int cover 0;//cover 表示每个位置能够覆盖的长度// 在 cover 个长度里边走不能超过 coverfor (int i 0; i cover; i) {cover Math.max(inums[i],cover);if(cover nums.length-1){return true;}}return false;}
}45.跳跃游戏II
题目链接
https://leetcode.cn/problems/jump-game-ii/description/
题目描述 思路
太难想了/(ㄒoㄒ)/~~ 没太听明白得再看看
class Solution {public int jump(int[] nums) {if (nums null || nums.length 0 || nums.length 1) {return 0;}//记录跳跃的次数int count0;//当前的覆盖最大区域int curDistance 0;//最大的覆盖区域int maxDistance 0;for (int i 0; i nums.length; i) {//在可覆盖区域内更新最大的覆盖区域maxDistance Math.max(maxDistance,inums[i]);//说明当前一步再跳一步就到达了末尾if (maxDistancenums.length-1){count;break;}//走到当前覆盖的最大区域时更新下一步可达的最大区域if (icurDistance){curDistance maxDistance;count;}}return count;}
}