【例题1】1260. 二维网格迁移 - 力扣(LeetCode)
/**
* 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** p = (int**)malloc(sizeof(int*)*gridSize);
for(int i = 0;i<gridSize;i++){
p[i] = (int*)malloc(sizeof(int)*(*gridColSize));
}
//实现逻辑
int i,j;
for(i=0;i<gridSize;i++){
for(j=0;j<*gridColSize;j++){
int value = ((i*(*gridColSize))+j+k)%(gridSize*(*gridColSize));
p[value/(*gridColSize)][value%(gridSize)] = grid[i][j];
}
}
//确定返回参数
*returnSize = gridSize;
*returnColumnSizes = (int*)malloc(sizeof(int)*gridSize);
for(i=0;i<gridSize;i++){
(*returnColumnSizes)[i] = *gridColSize;
}
return p;
}
【例题2】661. 图片平滑器 - 力扣(LeetCode)
/**
* 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 i,j;
// 创建数组
int** p = (int**)malloc(sizeof(int*)*imgSize);
for(i=0;i<imgSize;i++){
p[i] = (int*)malloc(sizeof(int)*(*imgColSize));
}
//实现逻辑
for(i=0;i<imgSize;i++){
for(j=0;j<*imgColSize;j++){
// 核心代码
int sum=0;int fenmu = 0;
for(int m=i-1;m<=i+1;m++){
for(int n=j-1;n<=j+1;n++){
if(m >=0 && m < imgSize && n>=0 && n<(*imgColSize)){
sum+=img[m][n];fenmu++;
}
}
}
p[i][j] = (int)(sum*1.0/fenmu);
}
}
//规定返回参数
*returnSize = imgSize;
*returnColumnSizes = (int*)malloc(sizeof(int)*imgSize);
for(i=0;i<imgSize;i++){
(*returnColumnSizes)[i] = *imgColSize;
}
return p;
}
【例题3】1314. 矩阵区域和 - 力扣(LeetCode)
/**
* 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** matrixBlockSum(int** mat, int matSize, int* matColSize, int k, int* returnSize, int** returnColumnSizes){
// 创建数组
int** p = (int**)malloc(sizeof(int*)*matSize);
for(int i=0;i<matSize;i++){
p[i] = (int*)malloc(sizeof(int)*(*matColSize));
}
// 实现逻辑
for(int i=0;i<matSize;i++){
for(int j=0;j<(*matColSize);j++){
int sum = 0;
for(int m=i-k;m<=i+k;m++){
for(int n=j-k;n<=j+k;n++){
if(m>=0 && m<matSize && n>=0 && n<(*matColSize)){
sum += mat[m][n];
}
}
}
p[i][j] = sum;
}
}
// 规定返回参数值
*returnSize = matSize;
*returnColumnSizes = (int*)malloc(sizeof(int)*matSize);
for(int i=0;i<matSize;i++){
(*returnColumnSizes)[i] = *matColSize;
}
return p;
}
【例题4】LCP 44. 开幕式焰火 - 力扣(LeetCode)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int arr[1001];
void transer(struct TreeNode* root){
if(root == NULL) return;
arr[root->val] = 1;
transer(root->left);
transer(root->right);
}
int numColor(struct TreeNode* root){
if(root == NULL) return 0;
memset(arr,0,sizeof(arr));
transer(root);
int ans = 0;
for(int i=0;i<sizeof(arr)/sizeof(int);i++){
if(arr[i] == 1) ans++;
}
return ans;
}
【例题5】397. 整数替换 - 力扣(LeetCode)
int integerReplacement(int n){
unsigned int m = n;
if(m==1) return 0;
if(m%2==0) return 1+integerReplacement(m/2);
else return 1+integerReplacement(m-1)<1+integerReplacement(m+1)?1+integerReplacement(m-1):1+integerReplacement(m+1);
}