day17_LC学习计划:数据结构基础

本文介绍了杨辉三角的递推解法,详细解析了如何在O(rowIndex)空间复杂度内实现,并探讨了旋转图像的原地旋转策略以及螺旋矩阵的生成算法,是数据结构学习的良好实践。

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

119.杨辉三角 Ⅱ

给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。

在「杨辉三角」中,每个数是它左上方和右上方的数的和。

示例 1:

输入: rowIndex = 3
输出: [1,3,3,1]
示例 2:

输入: rowIndex = 0
输出: [1]
示例 3:

输入: rowIndex = 1
输出: [1,1]
 

提示:

0 <= rowIndex <= 33
 

进阶:

你可以优化你的算法到 O(rowIndex) 空间复杂度吗?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/pascals-triangle-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

一、idea:

想法是在利用数组的一行数组每次放入杨辉三角n的信息,在遍历n+1时用两个变量保存n行的信息(为什么是两个,因为n+1行的数据是由n行中的两个数得到的),计算出n+1行的信息后更新数组。

二、解法:

  1. 递推
  2. 线性递推

三、代码实现细节的学习:

1、整个杨辉三角满足,所以在每一行倒着处理即可得到。

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* getRow(int rowIndex, int* returnSize){
    *returnSize=rowIndex+1;
    int *arr=malloc(sizeof(int)*(rowIndex+1));
    memset(arr,0,sizeof(int)*(*returnSize));
    arr[0]=1;
    for(int i=1;i<=rowIndex;i++){
        for(int j=i;j>0;j--){
            arr[j]=arr[j]+arr[j-1];
        }
    }
    return arr;
}

2、利用组合数公式递推

出现在数字后带着LL,表示long long类型,当数字大于2^31-1时都需要带着LL。

48.旋转图像

给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

示例 1:


输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[[7,4,1],[8,5,2],[9,6,3]]
示例 2:


输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
 

提示:

n == matrix.length == matrix[i].length
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000
 

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-image
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

一、idea:

二、解法:

  1. 使用辅助数组
  2. 原地旋转
  3. 用反转代替旋转

三、代码实现细节的学习: 

1、直接找规律,第i行的数第j个数等于倒数第i列的第j个数。计算倒数的列数时记得用行列数。

void rotate(int** matrix, int matrixSize, int* matrixColSize){
    int matrix_new[matrixSize][*matrixColSize];
    for(int i=0;i<matrixSize;i++){
        for(int j=0;j<*matrixColSize;j++){
            matrix_new[i][j]=matrix[i][j];
        }
    }
    for(int i=0;i<matrixSize;i++){
        for(int j=0;j<*matrixColSize;j++){
            matrix[j][matrixSize-i-1]=matrix_new[i][j];
        }
    }
}

2、 分析规律,旋转90度,则旋转4次会回到原点,那么只需要把整体的矩阵分为四块再进行旋转就可以了。并且观察可以发现,当矩阵的边为奇数和偶数时,会得到不一样的结果。分析发现只需要把列+1或行+1即可包含矩阵边为奇数的情况。



void rotate(int** matrix, int matrixSize, int* matrixColSize){
    for(int i=0;i<(matrixSize+1)/2;i++){
        for(int j=0;j<(*matrixColSize)/2;j++){
             int temp = matrix[i][j];
            matrix[i][j] = matrix[matrixSize - j - 1][i];
            matrix[matrixSize - j - 1][i] = matrix[matrixSize - i - 1][matrixSize - j - 1];
            matrix[matrixSize - i - 1][matrixSize - j - 1] = matrix[j][matrixSize - i - 1];
            matrix[j][matrixSize - i - 1] = temp;
        }
    }
}

3、直接反转,熟悉一下矩阵性质,反转和斜对角。

void rotate(int** matrix, int matrixSize, int* matrixColSize){
    void swap(int* a, int* b) {
    int t = *a;
    *a = *b, *b = t;
}

    // 水平翻转
    for (int i = 0; i < matrixSize / 2; ++i) {
        for (int j = 0; j < matrixSize; ++j) {
            swap(&matrix[i][j], &matrix[matrixSize - i - 1][j]);
        }
    }
    // 主对角线翻转
    for (int i = 0; i < matrixSize; ++i) {
        for (int j = 0; j < i; ++j) {
            swap(&matrix[i][j], &matrix[j][i]);
        }
    }
}

59.螺旋矩阵 Ⅱ

给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。

示例 1:


输入:n = 3
输出:[[1,2,3],[8,9,4],[7,6,5]]
示例 2:

输入:n = 1
输出:[[1]]
 

提示:

1 <= n <= 20

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/spiral-matrix-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

一、idea:

按圈进行遍历放置数字。

二、解法:

  1. 模拟
  2. 按层遍历

三、代码实现细节的学习: 

1、这种分成四个方向移动的模拟方法应该学习一下。



/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){
    *returnSize=n;
    *returnColumnSizes=malloc(sizeof(int)*n);
    int** ret=malloc(sizeof(int*)*n);
    for(int i=0;i<n;i++){
        ret[i]=malloc(sizeof(int)*n);
        memset(ret[i],0,sizeof(int)*n);
        (*returnColumnSizes)[i]=n;
    }
    int count=1;
    int row=0,col=0;
    int direction[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
    int directionIndex=0;
    while(count<=n*n){
        ret[row][col]=count;
        count++;
        int newRow=row+direction[directionIndex][0],newCol=col+direction[directionIndex][1];
        if(newRow<0||newRow>n-1||newCol<0||newCol>n-1||ret[newRow][newCol]!=0){
            directionIndex=(directionIndex+1)%4;
        }
        row=row+direction[directionIndex][0];
        col=col+direction[directionIndex][1];
    }
    return ret;
}

2、学习一下二维数组的初始化方式。

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){
    *returnSize=n;
    int** ret=malloc(sizeof(int*)*n);
    *returnColumnSizes=malloc(sizeof(int)*n);
    for(int i=0;i<n;i++){
        ret[i]=malloc(sizeof(int)*n);
        memset(ret[i],0,sizeof(int)*n);
        (*returnColumnSizes)[i]=n;
    }
    int count=1;
    int left=0,right=n-1,top=0,bottom=n-1;
    while(left<=right&&top<=bottom){
        //最好第一行直接遍历到底,否则不会进入第二圈
        for(int col=left;col<=right;col++){
            ret[top][col]=count;
            count++;
        }
        //以下根据第一行进行匹配
        for(int row=top+1;row<=right;row++){
            ret[row][right]=count;
            count++;
        }
        for(int col=right-1;col>left;col--){
            ret[bottom][col]=count;
            count++;
        }
        for(int row=bottom;row>top;row--){
            ret[row][left]=count;
            count++;
        }
        left++;
        right--;
        top++;
        bottom--;
    }
    return ret;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值