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;
}