算法训练营第二天|977.有序数组的平方 209.长度最小的子数组 59.螺旋矩阵II

LeetCode977.有序数组的平方

文档讲解:代码随想录
视频讲解:双指针法经典题目 | LeetCode:977.有序数组的平方
题目链接:LeetCode977.有序数组的平方

思路:一种是暴力排序,如下:

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int l = 0;
        int r = nums.size() - 1;
        vector<int> numsnew(nums.size(), 0);
        for(int i = nums.size() - 1; i >= 0; i--){
            if (nums[l] * nums[l] <= nums[r] * nums[r]){
                numsnew[i] = nums[r] * nums[r];
                r--;
            }
            else {
                numsnew[i] = nums[l] * nums[l];
                l++;
            }
        }
        return numsnew;
    }
};

一种是新开一个数组,用双向指针排序

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int l = 0;
        int r = nums.size() - 1;
        vector<int> numsnew(nums.size(), 0);
        for(int i = nums.size() - 1; i >= 0; i--){
            if (nums[l] * nums[l] <= nums[r] * nums[r]){
                numsnew[i] = nums[r] * nums[r];
                r--;
            }
            else {
                numsnew[i] = nums[l] * nums[l];
                l++;
            }
        }
        return numsnew;
    }
};

思考:数据有规律,不是无序,最后又要求顺序排列,这是可以想到双指针。

LeetCode209.长度最小的子数组

文档讲解:代码随想录
视频讲解:拿下滑动窗口! | LeetCode 209 长度最小的子数组
题目链接:LeetCode209.长度最小的子数组

思路:一种是双层循环暴力解:另一种考虑到选出连续子序列,想到快慢指针,如下:

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int result = INT32_MAX;
        int sum = 0;
        int s = 0;
        int sumlength;
        for(int f = 0; f < nums.size(); f++){
            sum += nums[f];
            while (sum >= target){
                sumlength = f - s + 1;
                result = result < sumlength ? result : sumlength;
                sum -= nums[s++];
            }            
        }
        return result == INT32_MAX ? 0 : result;
    }
};

双指针妙用很多,还需要多刷题多体会。。

LeetCode59.螺旋矩阵II

文档讲解:代码随想录
视频讲解:一入循环深似海 | LeetCode:59.螺旋矩阵II
题目链接:59.螺旋矩阵II

思路:主要是想好构造方法,注意原则一致(左闭右开)

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> result (n, vector<int>(n, 0));
        int startx = 0, starty = 0;
        int offset = 1;
        int loop = n / 2;
        int mid = n / 2;
        int count = 1;
        while (loop--){
            int i = startx, j = starty;

            for(; j < n - offset; j++){
                result[i][j] = count++;
            }
            for(; i < n - offset; i++){
                result[i][j] = count++;
            }
            for(; j > startx; j--){
                result[i][j] = count++;
            }
            for(; i > starty; i--){
                result[i][j] = count++;
            }
            startx++;
            starty++;
            offset++;
        }
        if (n % 2){
            result[mid][mid] = count;
        }
        return result;
    }
};

第二天打卡,刷题和写博客都比昨天熟练了许多,加油!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值