代码随想录算法训练营Day32 | 122.买卖股票的最佳时机II,55. 跳跃游戏,45.跳跃游戏II | Day1,2 复习

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

文章链接  |  题目链接  |  视频链接

C++解法

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int result = 0;
        for (int i = 1; i < prices.size(); i++) {
            result += max(prices[i] - prices[i - 1], 0);
        }
        return result;
    }
};

Python解法

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        profit = 0
        for i in range(1, len(prices)):
            if prices[i] - prices[i-1] > 0:
                profit += prices[i] - prices[i-1]
        return profit

55. 跳跃游戏

文章链接  |  题目链接  |  视频链接

C++解法

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int cover = 0;
        if (nums.size() == 1) return true;
        for (int i = 0; i <= cover; i++){
            cover = max(cover, nums[i] + i);
            if (cover >= nums.size() - 1){
                return true;
            }
        }
        return false;     
    }
};

Python解法

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        cover = 0
        if len(nums) == 1:
            return True
        i = 0
        while i <= cover:
            cover = max(cover, i + nums[i])
            if cover >= len(nums) - 1:
                return True
            i += 1
        return False

45.跳跃游戏II 

文章链接  |  题目链接  |  视频链接

C++解法

class Solution {
public:
    int jump(vector<int>& nums) {
        if (nums.size() == 1) return 0;
        int curDistance = 0;
        int ans = 0;
        int nextDistance = 0;
        for (int i = 0; i < nums.size(); i++){
            nextDistance = max(nums[i] + i, nextDistance);
            if (i == curDistance){
                ans++;
                curDistance = nextDistance;
                if (nextDistance >= nums.size() - 1) break;
            }           
        }
        return ans;
    }
};

Python解法

class Solution:
    def jump(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return 0
        ans = 0
        curr = 0
        next = 0
        for i in range(len(nums)):
            next = max(next, i + nums[i])
            if i == curr:
                curr = next
                ans += 1
                if next >= len(nums)-1:
                    break
        return ans

Day1

704. 二分查找

题目链接

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1;
        while (left <= right){
            int mid = left + (right - left) / 2;
            if (nums[mid] == target){
                return mid;
            } else if (nums[mid] > target){
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }
};

27. 移除元素 

题目链接

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int k = 0;
        for (int i = 0; i < nums.size(); i++){
            if (nums[i] != val){
                nums[k] = nums[i];
                k++;
            } 
        }
        return k;
    }
};

Day 2

977.有序数组的平方

题目链接

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int left = 0;
        int right = nums.size() - 1;
        vector<int> squared_nums(nums.size());
        int k = nums.size() - 1;
        while (left <= right){
            int left_sqr = nums[left] * nums[left];
            int right_sqr = nums[right] * nums[right];
            if (left_sqr > right_sqr){
                squared_nums[k] = left_sqr;
                k--;
                left++;
            } else {
                squared_nums[k] = right_sqr;
                right --;
                k--;
            }
        }
        return squared_nums;
    }
};

209.长度最小的子数组

题目链接

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int result = INT_MAX;
        int length = 0;
        int sum = 0;
        for (int i = 0; i < nums.size(); i++){
            sum += nums[i];
            length++;
            while (sum >= target){
                result = min(result, length);
                sum -= nums[i-length+1];
                length--; 
            }
        }
        if (result != INT_MAX){
            return result;
        } else {
            return 0;
        }        
    }
};

59.螺旋矩阵II

题目链接

skipped

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值