九日集训 Day 8 二级指针

本文介绍了C++中的二级指针概念,并提供了内存分配的模板代码。同时,详细讲解了LeetCode中涉及矩阵转置、矩阵重塑和其他矩阵操作的题目,包括翻转图像、转置矩阵、网格迁移和图片滤波等,每道题目都给出了相应的解决方案和关键代码片段。
摘要由CSDN通过智能技术生成

1 学习内容 二级指针

二级指针就是指针的指针,也就是说,二级指针的内容是一级指针的地址。

在指针中,涉及到内存的申请,这里附上模板。

int **myMalloc(int r, int c, int* returnSize, int** returnColumnSizes) {
    int i;
    int **ret = (int **)malloc( sizeof(int *) * r );        // (1)
    *returnColumnSizes = (int *)malloc( sizeof(int) * r );  // (2)
    *returnSize = r;                                        // (3)
    for(i = 0; i < r; ++i) {
        ret[i] = (int *)malloc( sizeof(int) * c );          // (4)
        (*returnColumnSizes)[i] = c;                        // (5)
    }    
    return ret;
}

此模板内容在博主——英雄哪里出来,九日集训专栏第八讲中复制而来,侵删。

2 Leetcode 刷题

2.1 矩阵转置

相关题目

翻转图像

转置矩阵

第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 **myMalloc(int r, int c, int* returnSize, int** returnColumnSizes) {
    int i;
    int **ret = (int **)malloc( sizeof(int *) * r );        
    *returnColumnSizes = (int *)malloc( sizeof(int) * r );  
    *returnSize = r;                                        
    for(i = 0; i < r; ++i) {
        ret[i] = (int *)malloc( sizeof(int) * c );          
        (*returnColumnSizes)[i] = c;                        
    }    
    return ret;
}

int** flipAndInvertImage(int** image, int imageSize, int* imageColSize, int* returnSize, int** returnColumnSizes){

    int r= imageSize, c = imageColSize[0];
    int **ret = myMalloc(r,c,returnSize, returnColumnSizes);
    for(int i = 0; i<r; ++i){
        for(int j =0; j<c; ++j){
            ret[i][j] = 1- image[i][c-1-j];
        }
    }
    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 **myMalloc(int r, int c, int* returnSize, int** returnColumnSizes) {
    int i;
    int **ret = (int **)malloc( sizeof(int *) * r );        // (1)
    *returnColumnSizes = (int *)malloc( sizeof(int) * r );  // (2)
    *returnSize = r;                                        // (3)
    for(i = 0; i < r; ++i) {
        ret[i] = (int *)malloc( sizeof(int) * c );          // (4)
        (*returnColumnSizes)[i] = c;                        // (5)
    }    
    return ret;
}

int** transpose(int** matrix, int matrixSize, int* matrixColSize, int* returnSize, int** returnColumnSizes){
    int r = matrixSize;
    int c = matrixColSize[0];
    int **ret = myMalloc(c, r, returnSize, returnColumnSizes);
    for (int i = 0; i<r; ++i){
        for(int j = 0; j<c; ++j){
            ret[j][i] = matrix[i][j];
        }
    }
    return ret;
}

2.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 **myMalloc(int r, int c, int* returnSize, int** returnColumnSizes) {
    int i;
    int **ret = (int **)malloc( sizeof(int *) * r );        // (1)
    *returnColumnSizes = (int *)malloc( sizeof(int) * r );  // (2)
    *returnSize = r;                                        // (3)
    for(i = 0; i < r; ++i) {
        ret[i] = (int *)malloc( sizeof(int) * c );          // (4)
        (*returnColumnSizes)[i] = c;                        // (5)
    }    
    return ret;
}

int** matrixReshape(int** mat, int matSize, int* matColSize, int r, int c, int* returnSize, int** returnColumnSizes){
    int n = matSize;
    int m = matColSize[0];
    int **ret, id;
    ret = myMalloc(r, c, returnSize, returnColumnSizes);
    if(n*m != r *c){
        *returnSize = n;
        for(int i = 0; i<n; ++i){
            (*returnColumnSizes)[i] = m;
        
        }
        return mat;
    }
    for(int i = 0; i<r; ++i){
        for(int j = 0; j< c; ++j){
            id = i*c + j;
            ret[i][j] = mat[id/m][id%m];
        }
    }
    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 **myMalloc(int r, int c, int* returnSize, int** returnColumnSizes) {
    int i;
    int **ret = (int **)malloc( sizeof(int *) * r );        // (1)
    *returnColumnSizes = (int *)malloc( sizeof(int) * r );  // (2)
    *returnSize = r;                                        // (3)
    for(i = 0; i < r; ++i) {
        ret[i] = (int *)malloc( sizeof(int) * c );          // (4)
        (*returnColumnSizes)[i] = c;                        // (5)
    }    
    return ret;
}

int** construct2DArray(int* original, int originalSize, int m, int n, int* returnSize, int** returnColumnSizes){
    int **ret, i, j;
    if(originalSize != n*m){
        *returnSize = 0;
        return ret;
    }
    ret = myMalloc(m, n, returnSize, returnColumnSizes);
    for(i =0; i<m; ++i){
        for(j = 0;j<n; ++j){
            ret[i][j] = original[i*n+j];
        }
    }
    return ret;
}

2.3 其他矩阵操作

网格迁移

图片滤波

矩阵区域和

排列矩阵单元格

第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 **myMalloc(int r, int c, int* returnSize, int** returnColumnSizes) {
    int i;
    int **ret = (int **)malloc( sizeof(int *) * r );        // (1)
    *returnColumnSizes = (int *)malloc( sizeof(int) * r );  // (2)
    *returnSize = r;                                        // (3)
    for(i = 0; i < r; ++i) {
        ret[i] = (int *)malloc( sizeof(int) * c );          // (4)
        (*returnColumnSizes)[i] = c;                        // (5)
    }    
    return ret;
}

int** shiftGrid(int** grid, int gridSize, int* gridColSize, int k, int* returnSize, int** returnColumnSizes){
    int i, j;
    int m = gridSize;
    int n = gridColSize[0];
    int **ans = myMalloc(m, n ,returnSize, returnColumnSizes);
    
    k %= m*n;
    

    for(i = 0; i < m; ++i){
        for(j = 0; j<n; ++j){
            int id =  (m * n + i * n + j - k ) % (m * n);
            ans[i][j] = grid[id / n][id % n];
        }
    }

    return ans;
}

第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 **myMalloc(int r, int c, int* returnSize, int** returnColumnSizes) {
    int i;
    int **ret = (int **)malloc( sizeof(int *) * r );        // (1)
    *returnColumnSizes = (int *)malloc( sizeof(int) * r );  // (2)
    *returnSize = r;                                        // (3)
    for(i = 0; i < r; ++i) {
        ret[i] = (int *)malloc( sizeof(int) * c );          // (4)
        (*returnColumnSizes)[i] = c;                        // (5)
    }    
    return ret;
}
int** imageSmoother(int** img, int imgSize, int* imgColSize, int* returnSize, int** returnColumnSizes){
    int r = imgSize;
    int c = imgColSize[0];
    int i, j;
    
    int **ans = myMalloc(r, c, returnSize, returnColumnSizes);
    for(i = 0; i<r; ++i){
        for(j = 0; j< c; ++j){
            int total_num = 0;
            int count_num = 0;
            for(int x = i -1;x<=i+1; ++x){
                for(int y = j-1; y<=j+1;++y){
                    if(x>=0 && x<r &&y>=0 && y<c)
                    {
                        total_num += img[x][y];
                        ++count_num;
                    }
                }
            }
            ans[i][j] = total_num / count_num;
        }
    }
    return ans;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值