【例题1】1252. 奇数值单元格的数目 - 力扣(LeetCode)

int arr[50][50];
int oddCells(int m, int n, int** indices, int indicesSize, int* indicesColSize){
    memset(arr,0,sizeof(arr));
    for(int i=0;i<indicesSize;i++){
        int r = indices[i][0];
        int c = indices[i][1];
        //横向
        for(int k=0;k<n;k++) arr[r][k]+=1;
        // 纵向
        for(int k=0;k<m;k++) arr[k][c]+=1;      
    }
    int ans = 0;
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(arr[i][j] % 2==1) ans++;
        }
    }
    return ans;
}

【例题2】657. 机器人能否返回原点 - 力扣(LeetCode)

bool judgeCircle(char * moves){
    int arr[2] = {0,0};
    // 左 右  上 下
    int len = strlen(moves);
    for(int i=0;i<len;i++){
        switch(moves[i]){
            case 'L': arr[0]--;break;
            case 'R': arr[0]++;break;
            case 'U': arr[1]--;break;
            case 'D': arr[1]++;break;
        }
    }
    if(arr[0] == 0 && arr[1]==0) return true;
    return false;
}

【例题3】289. 生命游戏 - 力扣(LeetCode)

int arr[25][25];
void gameOfLife(int** board, int boardSize, int* boardColSize){
    int i,j,x,y,count;
    for(i=0;i<boardSize;i++){
        for(j=0;j<*boardColSize;j++){
            arr[i][j] = board[i][j];
        }
    }
    for(i=0;i<boardSize;i++){
        for(j=0;j<*boardColSize;j++){
            count = 0;
            for(x=-1;x<=1;x++){
                for(y=-1;y<=1;y++){
                    if(x==0 && y==0) continue;
                    if(x+i <0 || x+i >= boardSize 
                    || y+j <0 || y+j >= *boardColSize) continue;
                    if(arr[x+i][y+j] == 1) count++;
                }
            }
            if(count == 3) {
                board[i][j] = 1;
            }else{
                if(count < 2 || count > 3){
                    board[i][j] = 0;
                }else{
                    board[i][j] = arr[i][j];
                }
            }
            
        }
    }
}

【例题4】1441. 用栈操作构建数组 - 力扣(LeetCode)

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
char ** buildArray(int* target, int targetSize, int n, int* returnSize){
    char** ans = (char**)malloc(sizeof(char*)*200);
    int num=1;
    int len=0;
    for(int i=0;i<targetSize;num++){
        //不是目标数
        if(num!=target[i]){
            ans[len++] = "Push";
            ans[len++] = "Pop";
        }
        //是目标数
        if(num == target[i]){
            ans[len++] = "Push";
            i++;
        }
    }
    *returnSize = len;
    return ans;
}

【例题5】59. 螺旋矩阵 II - 力扣(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().
 */


 //              B
 // A   1  2  3  4
 //    12 13 14  5 
 //    11 16 15  6
 //    10  9  8  7  C
 //     D          


void run(int** m,int val,int n){
    int i,A = 0,D = 0;
    int B = n-1,C = n-1;
    while(A < C && D < B){
        // 横行 上
        for(i=D;i<B;i++) m[A][i] = val++;
        // 竖行 右
        for(i=D;i<C;i++) m[i][B] = val++;
        // 横航 下
        for(i=B;i>D;i--) m[C][i] = val++;
        // 竖行 左
        for(i=C;i>A;i--) m[i][D] = val++;
        A++;B--;C--;D++;
    }
    if(A==C && B==D) m[A][D] = val;
}

int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){
    //赋值
    int** matrix = (int**)malloc(sizeof(int*)*n);
    for(int i=0;i<n;i++){
        matrix[i] = (int*)malloc(sizeof(int)*n);
    }
    //规定返回值
    *returnSize = n;
    *returnColumnSizes = (int*)malloc(sizeof(int)*n);
    for(int i=0;i<n;i++){
        (*returnColumnSizes)[i]=n;
    }
    //主代码逻辑
    int i=1;
    run(matrix,i,n);
    return matrix;
}