数组-对角线遍历

文章提供了两段C语言代码,用于实现矩阵的对角线遍历。第一段代码包含错误,主要问题在于比较操作符使用不当。第二段代码是修复后的版本,能够正确处理边界条件,避免溢出。修复后的代码保证了在遍历过程中正确地沿着对角线移动并处理矩阵边缘的情况。
摘要由CSDN通过智能技术生成

对角线遍历

描述:https://leetcode.cn/leetbook/read/array-and-string/cuxq3/

错误代码

c语言代码⬇️

int* findDiagonalOrder(int** mat, int matSize, int* matColSize) {
    int* ret = (int*)malloc(sizeof(int) * matSize * (*matColSize));

    int idx = 0;
    int row = 0;
    int col = 0;

    for (int i = 0; i <= matSize + (*matColSize); i++) {
        if (i % 2 == 0) {
            while (row >= 0 && 
                    row < matSize &&
                    col < *matColSize &&
                    col >= 0) {
                ret[idx++] = mat[row][col];
                row--;
                col++;
            }
            //如果往右上角溢出
            if (row < 0 && col == *matColSize) {
                row = 1;
                col = *matColSize - 1;
            }
            //如果在上边溢出
            if (row < 0) {
                row = 0;
            }
            //如果在右边溢出
            if (col == *matColSize) {
                col -= 1;
                row += 2;
            }
        }
        else {
            while (row >= 0 &&
                row < matSize &&
                col < *matColSize &&
                col >= 0) {
                ret[idx++] = mat[row][col];
                row++;
                col--;
            }
            //如果在左下角溢出
            if (row = matSize && col < 0) {
                row = matSize - 1;
                col = 1;
            }
            //如果在下边溢出
            if (row = matSize) {
                row -= 1;
                col += 2;
            }
            //如果在左边溢出
            if (col < 0) {
                col += 1;
            }
        }
    }

    return ret;
}

错误原因:sir. 请不要在判断语句中将==写成=,这非常的不professional

通过代码

通过代码1⬇️

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* findDiagonalOrder(int** mat, int matSize, int* matColSize, int* returnSize){
    int* ret = (int*)malloc(sizeof(int) * matSize * (*matColSize));
    *returnSize = matSize * (*matColSize);
    int idx = 0;
    int row = 0;
    int col = 0;

    for (int i = 0; i <= matSize + (*matColSize); i++) {
        if (i % 2 == 0) {
            while (row >= 0 && 
                    row < matSize &&
                    col < *matColSize &&
                    col >= 0) {
                ret[idx++] = mat[row][col];
                row--;
                col++;
            }
            //如果往右上角溢出
            if (row < 0 && col == *matColSize) {
                row = 1;
                col = *matColSize - 1;
            }
            //如果在上边溢出
            if (row < 0) {
                row = 0;
            }
            //如果在右边溢出
            if (col == *matColSize) {
                col -= 1;
                row += 2;
            }
        }
        else {
            while (row >= 0 &&
                row < matSize &&
                col < *matColSize &&
                col >= 0) {
                ret[idx++] = mat[row][col];
                row++;
                col--;
            }
            //如果在左下角溢出
            if (row == matSize && col < 0) {
                row = matSize - 1;
                col = 1;
            }
            //如果在下边溢出
            if (row == matSize) {
                row -= 1;
                col += 2;
            }
            //如果在左边溢出
            if (col < 0) {
                col += 1;
            }
        }
    }

    return ret;
}

通过代码2⬇️


总结

总结:n

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值