Day2 有序数组的平方 + 长度最小的子数组 + 螺旋矩阵Ⅱ

977 有序数组的平方

题目链接:977. 有序数组的平方 - 力扣(LeetCode)

题目链接:

给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。

输入:nums = [-4,-1,0,3,10]
输出:[0,1,9,16,100]
解释:平方后,数组变为 [16,1,0,9,100],排序后,数组变为 [0,1,9,16,100]

思路:
使用双指针,在ans数组中依次放入大的值

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int len = nums.size();
        int cnt = len - 1;
        vector<int> ans(len, 0);
        int i = 0, j = len - 1;
        while(i <= j)
        {
            if(nums[i] * nums[i] > nums[j] * nums[j])
            {
                ans[cnt] = nums[i] * nums[i];
                i++;
                cnt--;
            }
            else
            {
                ans[cnt] = nums[j] * nums[j];
                j--;
                cnt--;
            }
        }
        return ans;
    }
};

209 长度最小的子数组

题目链接:209. 长度最小的子数组 - 力扣(LeetCode)

给定一个含有 n 个正整数的数组和一个正整数 target 。找出该数组中满足其总和大于等于 target 的长度最小的 连续子数组,并返回其长度 。 如果不存在符合条件的子数组,返回 0 。

输入:target = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。

方法一 滑动窗口

都快忘记滑动窗口这个做法了,笔试碰到了好几次连续子数组的题了。

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int ans = INT_MAX;
        int sum = 0;
        int i = 0, len = nums.size();
        for(int j = 0; j < len; j++)
        {
            sum += nums[j];
            while(sum >= target)
            {
                int sublen = j - i + 1;
                ans = min(ans, sublen);
                sum -= nums[i];
                i++;
            }
        }
        return ans == INT_MAX ? 0 : ans;
    }
};

方法二 前缀和 + 二分查找

找i,j,使得s[j] - s[i] >= target,等同于找s[j] >= target + s[i]

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size();
        if(n == 0) return 0;
        int ans = INT_MAX;
        vector<int> sum(n + 1, 0);
        for(int i = 1; i <= n; i++)
        {
            sum[i] = sum[i - 1] + nums[i - 1];
        }
        for(int i = 1; i <= n; i++)
        {
            int temp = target + sum[i - 1];
            auto bound = lower_bound(sum.begin(), sum.end(), temp);
            if(bound != sum.end())
            {
                ans = min(ans, static_cast<int>((bound - sum.begin()) - (i - 1)));
            }
        }
        return ans == INT_MAX ? 0 : ans;
    }
};

59 螺旋矩阵Ⅱ

题目链接:59. 螺旋矩阵 II - 力扣(LeetCode)

确定上下左右边界,按照螺旋顺序放置在对应位置

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        int l = 0, r = n - 1, t = 0, b = n - 1;
        vector<vector<int>> ans(n, vector<int>(n, 0));
        int x = 1;
        while(true)
        {
            for(int i = l; i <= r; i++) //left to right
            {
                ans[t][i] = x;
                x++;
            }
            if (++t > b) break;

            for(int i = t; i <= b; i++) //top to bottom
            {
                ans[i][r] = x;
                x++;
            }
            if(l > --r) break;

            for(int i = r; i >= l; i--) //right to left
            {
                ans[b][i] = x;
                x++;
            }
            if(t > --b) break;

            for(int i = b; i >= t; i--) //bottom to top
            {
                ans[i][l] = x;
                x++;
            }
            if(++l > r) break;
        }
        return ans;
    }
};
  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个用C语言编写的二维数组矩阵相乘的代码示例: ``` #include <stdio.h> #include <stdlib.h> typedef struct { int rows; int columns; int **elements; } TwoDArray, *TwoDArrayPtr; TwoDArrayPtr initTwoDArray(int rows, int columns) { TwoDArrayPtr arrayPtr = (TwoDArrayPtr)malloc(sizeof(TwoDArray)); arrayPtr->rows = rows; arrayPtr->columns = columns; arrayPtr->elements = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { arrayPtr->elements[i = (int*)malloc(columns * sizeof(int)); } return arrayPtr; } void matrixMultiply(TwoDArrayPtr paraPtr1, TwoDArrayPtr paraPtr2, TwoDArrayPtr *resultPtr) { if (paraPtr1->columns != paraPtr2->rows) { printf("矩阵不能相乘.\r\n"); *resultPtr = NULL; return; } *resultPtr = initTwoDArray(paraPtr1->rows, paraPtr2->columns); for (int i = 0; i < paraPtr1->rows; i++) { for (int j = 0; j < paraPtr2->columns; j++) { int sum = 0; for (int k = 0; k < paraPtr1->columns; k++) { sum += paraPtr1->elements[i][k * paraPtr2->elements[k][j]; } (*resultPtr)->elements[i][j = sum; } } } int main() { TwoDArrayPtr arrayPtr1, arrayPtr2, resultPtr; // 初始化两个矩阵 arrayPtr1 = initTwoDArray(2, 3); arrayPtr2 = initTwoDArray(3, 2); // 对两个矩阵赋值 // 矩阵相乘 matrixMultiply(arrayPtr1, arrayPtr2, &resultPtr); // 打印结果矩阵 for (int i = 0; i < resultPtr->rows; i++) { for (int j = 0; j < resultPtr->columns; j++) { printf("%d ", resultPtr->elements[i][j]); } printf("\n"); } // 释放内存 free(arrayPtr1); free(arrayPtr2); free(resultPtr); return 0; } ``` 这段代码使用了结构体`TwoDArray`来表示二维数组,并定义了`initTwoDArray`函数来初始化二维数组。`matrixMultiply`函数用于计算两个矩阵的乘积,并将结果存储在`resultPtr`指针指向的内存中。最后,在`main`函数中进行了相应的操作和打印结果。 请注意,这只是一个示例代码,具体的实现可能因编程语言或具体需求而有所不同。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [数据结构-C语言代码 day9-二维数组矩阵乘法](https://blog.csdn.net/yydslty/article/details/124866964)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [如何将一个二维数组和一个三维数组矩阵相乘得到一个三维数组?](https://blog.csdn.net/weixin_42561476/article/details/112899444)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值