目录
1.翻转图像
//申请一个二维数组
int **myMalloc(int r,int c){
int **a=(int **)malloc(sizeof(int*)*r);
for(int i=0;i<r;i++){
a[i]=(int*)malloc(sizeof(int)*c);
}
return a;
}
void swap(int*a,int*b){
int c=*a;
*a=*b;
*b=c;
}
int** flipAndInvertImage(int** image, int imageSize, int* imageColSize, int* returnSize, int** returnColumnSizes) {
int n = imageSize;
int m=imageColSize[0];
int **a=myMalloc(n,m);
a=image;
for(int i = 0; i < n; i++){
for(int j = 0; j < m/2; j++){
swap(&a[i][j], &a[i][n-j-1]);
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
a[i][j] = (a[i][j] + 1) % 2;
}
}
*returnColumnSizes=imageColSize;
*returnSize = n;
return a;
}
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().
*/
//申请一个二维数组m*n
int **myArray(int m,int n){
int** a=(int **)malloc(sizeof(int*)*m);
for(int i=0;i<m;i++){
a[i]=(int*)malloc(sizeof(int)*n);
}
return a;
}
int** transpose(int** matrix, int matrixSize, int* matrixColSize, int* returnSize, int** returnColumnSizes) {
int m=matrixSize;
int n=matrixColSize[0];
int **a=myArray(n,m);//转置行列交换n*m
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
a[i][j]=matrix[j][i];
}
}
* returnSize=n;//返回二维数组的行数
*returnColumnSizes = (int*)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
(*returnColumnSizes)[i] = m; // 返回转置矩阵的列数
}
return a;
}
3.重塑矩阵
/**
* 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().
*/
//思路:1.如果m*n=r*c,表示可以重构
//2.如果不等于则没法重构
int **myArray(int m,int n){
int** a=(int **)malloc(sizeof(int*)*m);
for(int i=0;i<m;i++){
a[i]=(int*)malloc(sizeof(int)*n);
}
return a;
}
int** matrixReshape(int** mat, int matSize, int* matColSize, int r, int c, int* returnSize, int** returnColumnSizes) {
int m=matSize;
int n=matColSize[0];
if(m*n!=r*c){
* returnSize=m;
* returnColumnSizes=(int* )malloc(sizeof(int)*m);//要穿一个数组回去
for(int i=0;i<m;i++){
(*returnColumnSizes)[i]=n;
}
return mat;
}else{
int **a=myArray(r,c);//构造r*c的矩阵
//x=(行)*c+列,列数=x%c,行数=(x-列数)/c+
for(int i=0;i<m*n;i++){
a[(i-i%c)/c][i%c]=mat[i/n][i%n];
}
* returnSize=r;
* returnColumnSizes=(int* )malloc(sizeof(int)*r);//要穿一个数组回去
for(int i=0;i<r;i++){
(*returnColumnSizes)[i]=c;
}
return a;
}
}
4.一维变二维
/**
* 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** construct2DArray(int* original, int originalSize, int m, int n, int* returnSize, int** returnColumnSizes) {
int num=originalSize;
if(num!=m*n){
* returnSize=0;
* returnColumnSizes=0;
int **a=0;
return a;
}
//构造一个二维数组m*n
int **a=(int**)malloc(sizeof(int*)*m);
* returnSize=m;
* returnColumnSizes=(int*)malloc(sizeof(int)*m);
//a={{},{}...{}}m个
for(int i=0;i<m;i++){
a[i]=(int*)malloc(sizeof(int)*n);
(*returnColumnSizes)[i]=n;
}
//i=行数*n+列数
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j]=original[i*n+j];
}
}
return a;
}
5.二维网格迁移
/**
* 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** shiftGrid(int** grid, int gridSize, int* gridColSize, int k, int* returnSize, int** returnColumnSizes){
int m = gridSize, n = gridColSize[0];
int **ret = (int **)malloc(sizeof(int *) * m);
*returnColumnSizes = (int *)malloc(sizeof(int) * m);
for (int i = 0; i < m; i++) {
ret[i] = (int *)malloc(sizeof(int) * n);
(*returnColumnSizes)[i] = n;
}//申请二维数组ret
//x=(行数*n+列数+k)%(m*n)
//行数=x-列数
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int index1 = (i * n + j + k) % (m * n);
ret[index1 / n][index1 % n] = grid[i][j];
}
}
*returnSize = m;
return ret;
}
6.图像平滑器
/**
* 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** imageSmoother(int** img, int imgSize, int* imgColSize, int* returnSize, int** returnColumnSizes){
int m = imgSize, n = imgColSize[0];
int ** ret = (int **)malloc(sizeof(int *) * m);
*returnColumnSizes = (int *)malloc(sizeof(int) * m);
for (int i = 0; i < m; i ++) {
ret[i] = (int *)malloc(sizeof(int) * n);
memset(ret[i], 0, sizeof(int) * n);
(*returnColumnSizes)[i] = n;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {//遍历所有元素
int num = 0, sum = 0;
for (int x = i - 1; x <= i + 1; x++) {
for (int y = j - 1; y <= j + 1; y++) {//遍历每个元素的左中右
if (x >= 0 && x < m && y >= 0 && y < n) {//只有当下标在范围内时候才加
num++;
sum += img[x][y];
}
}
}
ret[i][j] = sum / num;
}
}
*returnSize = m;
return ret;
}