代码随想录算法训练营第二天 | 977. Squares of a Sorted Array, 209. Minimum Size Subarray Sum& 59. Spiral Matrix II

代码随想录算法训练营第二天 | 977. Squares of a Sorted Array, 209. Minimum Size Subarray Sum& 59. Spiral Matrix II

写在最前面的Notes

  1. Ok前段时间在忙学校的期末(借口),完全没跟上打卡的节奏,现在照自己节奏慢慢补上吧。。
  2. 因为暑研需要用C++,所以从现在开始一样的代码会用Python和C++两面开工都写一遍!
  3. 光荣加入新一期打卡因为其那面没跟上哈哈哈哈哈

977. Squares of a Sorted Array

第二次刷 秒了哈哈
题目链接:977. Squares of a Sorted Array
题目描述:

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

文章讲解:代码随想录:977.有序数组的平方
视频讲解:双指针法经典题目 | LeetCode:977.有序数组的平方
读完题思路:

这道题还是很好想的,经典双指针。重点是要想到虽然不能确定最小平方值的位置,但最大的平方值一定是在两端的,那就可以从大到小来填充。这道题用Python写没问题,C++ syntax在熟悉的过程中

先上暴力解法(O(nlog(n)):

class Solution {
public:
    vector<int> sortedSquares(vector<int>& A) {
        for (int i = 0; i < A.size(); i++) {
            A[i] *= A[i];
        }
        sort(A.begin(), A.end()); // 快速排序
        return A;
    }
};

双指针Python:

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        n = len(nums)
        i, j = 0, len(nums)-1
        res = [0] * n
        n -= 1
        while i <= j:
            if abs(nums[j]) > abs(nums[i]):
                res[n] = nums[j] * nums[j]
                j -= 1
            else:
                res[n] = nums[i] * nums[i]
                i += 1
            n -= 1


        return res

双指针C++:

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        vector<int> result(nums.size(), 0);
        int k = nums.size()-1;
        for(int i=0, j=nums.size()-1; i<=j;) {
            if(std::abs(nums[i]) > std::abs(nums[j])) {
                result[k--] = std::pow(nums[i], 2); //k-- operation means that the value of k is used first, and then k is decremented
                i++;
            } else {
                result[k--] = std::pow(nums[j], 2);
                j--;
            }
        }
        return result;
    }
};

209. Minimum Size Subarray Sum

第二遍勉勉强强写出来,熟练度保持的还算可以,继续加油!
题目链接:209. Minimum Size Subarray Sum
文章讲解:代码随想录:209 长度最小的子数组
视频讲解:拿下滑动窗口! | LeetCode 209 长度最小的子数组
读完题思路:

刚读完就有印象要用到滑动窗口,但是忘记了具体如何implement。上一次做还是上次。于是看了讲解又是一个恍然大悟的状态。

Python

class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        current = 0
        i = 0
        result = float("inf")
        for j in range(len(nums)):
            current += nums[j]
            while current >= target:
                l = j - i + 1
                result = min(l, result)
                current -= nums[i]
                i += 1
        
        return result if result != float("inf") else 0
        

C++

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

59. Spiral Matrix II

07/04/2024 这一题很不争气的还是不会,重看视频,重新写了。。。
题目链接:59. Spiral Matrix II
题目描述:

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n^2 in spiral order.
这题光看description比较难理解,需要看example

文章讲解:代码随想录:螺旋矩阵II
视频讲解:一入循环深似海 | LeetCode:59.螺旋矩阵II
读完题思路:

思路就是呢,没什么思路,脑子乱乱的感觉无从下手。。。没错就是菜。但是读懂了卡尔学长的解释,就是希望下次刷到不要脑袋空空了吧

Python

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        startx, starty = 0, 0
        offset = 1
        result = [[0] * n for _ in range(n)]
        count = 1
        loop = n // 2
        for _ in range(0, loop):
            for j in range(starty, n-offset):
                result[startx][j] = count
                count += 1
            for i in range(startx, n-offset):
                result[i][n-offset] = count
                count += 1
            for j in range(n-offset, starty, -1):
                result[n-offset][j] = count
                count += 1
            for i in range(n-offset, startx, -1):
                result[i][starty] = count
                count += 1
            startx += 1
            starty += 1
            offset += 1
        if n%2 == 1:
            result[n//2][n//2] = count 
        
        return result

C++

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

        while (loop --) {
            i = startx;
            j = starty;

            for (j; j < n-offset; j++) {
                res[i][j] = count++;
            }

            for (i; i < n-offset; i++) {
                res[i][j] = count++;
            }

            for (; j > starty; j--) {
                res[i][j] = count++;
            }

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

今日收获与总结

这一part先简略写了,赶上进度最重要,加油加油!
第二遍刷,吃老本也就只能吃到今天了,加油加油争取这一轮能跟上进度!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值