Day 2 | LeetCode 977.有序数组的平方, LeetCode 209. 长度最小的子数组, LeetCode 59. 螺旋矩阵 II

文章介绍了如何使用双指针策略解决LeetCode中的三个问题:有序数组的平方、长度最小的子数组和螺旋矩阵II,涉及到了数组操作、滚动窗口和边界判断技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode 977.有序数组的平方

1.从中间开始双指针
class Solution {
public:
    vector<int> result;
    vector<int> sortedSquares(vector<int>& nums) {
        int fast = 0;
        int slow = 0;
        for (; fast < nums.size() && nums[fast] < 0; fast++) {

        }
        //fast指向第一个>=0的下标
        slow = fast - 1; //slow就指向了负数中最大的,平方后最小的下标
        while (slow >= 0 && fast < nums.size()) {
            //从中间分别往左和往右移动指针,将小的数push进result
            int temp = pow(nums[slow], 2) < pow(nums[fast], 2) ? pow(nums[slow--], 2) :  pow(nums[fast++], 2);
            result.push_back(temp);
        }
        //两个while其实只执行一个
        while (slow >= 0) {
            result.push_back(pow(nums[slow--], 2));
        }
        while (fast < nums.size()) {
            result.push_back(pow(nums[fast++], 2));
        }
        
        return result;
    }
};
2.从两边开始双指针
class Solution {
public:

    vector<int> sortedSquares(vector<int>& nums) {
        vector<int> result(nums.size(), 0);
        int left = 0; //左右指针
        int right = nums.size() - 1;
        int len = nums.size();
        while (left <= right) {
            int temp = pow(nums[left], 2) > pow(nums[right], 2) ? pow(nums[left++], 2) : pow(nums[right--], 2);
            result[--len] = temp;
       }
       return result;
    }
};

LeetCode 209. 长度最小的子数组

滑动窗口 + 双指针
当窗口内的sum < target, 右指针right++, sum递增,直到sum >= target
当窗口内的sum >= target, 左指针left就一直加,同时sum以此递减,直到窗口内的sum < target
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int left = 0;
        int right = 0;
        int sum = 0;
        int minLength = INT_MAX;
        
        for(; right < nums.size(); right++) {
            sum += nums[right]; //sum+= 的操作因该在此处,不能在循环尾部
            while (sum >= target) {
                minLength = min(minLength, right - left + 1);
                sum -= nums[left++];
            }
        }
        //判断数组内是否有解。 有返回minLength, 无返回0
        return minLength == INT_MAX ? 0 : minLength;
    }
};

LeetCode 59. 螺旋矩阵 II

较考验Coding水平和边界判断能力 我跪了🥲
class Solution { 
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n, vector<int>(n, 0));
        int loop = n / 2;
        
        int xStart = 0;
        int yStart = 0;
        int bound = 1;
        int x = 0;
        int y = 0;
        int count = 1; //计数器
        int mid = n / 2; //中间位置
        while (loop--) {
            x = xStart;
            y = yStart;
            for (; y < n - bound; y++) {
                res[x][y] = count++;
            }
            for (; x < n - bound; x++) {
                res[x][y] = count++;
            }
            for (; y > xStart; y--) {
                res[x][y] = count++;
            }
            for (; x > yStart; x--) {
                res[x][y] = count++;
            }
            bound += 1;
            xStart++;
            yStart++;
        }
        if (n % 2 == 1) {
            //奇数矩阵
            res[mid][mid] = count;
        }
        return res;

    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值