Leetcode---二维数组问题

1380. 矩阵中的幸运数 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=LA92https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/

Leetcode---二维数组问题_第1张图片


1. 

   其中**matrix 表示二维数组,matrixSize代表二维数组第一维的大小,也就是可以理解成有多少行;int* matrixColSize是一个一维数组,代表每一行有多少列,即matColSize[0]代表第 0 行有matColSize[0]列,matColSize[1]代表第 1 行有matColSize[1]列,matColSize[i]代表第 i ii 行有matColSize[i]列,以此类推。


2. 

    定义一个行数组minrow[matrixSize] 和一个列数组maxcol[*matrixColSize]。

3. 

 第一次遍历:存下每行的最小值到数组

for(i=0;i

4. 

第二次遍历:存下每列的最大值到数组

for(i=0;i<*matrixColSize;i++)                       //存下每列的最大值到数组
{
    maxcol[i]=min;                                 
    for(j=0;j=maxcol[i])
        {
            maxcol[i]=matrix[j][i];
        }
    }
}

5. 

 第三次遍历:满足条件的加入要返回的结果数组

*returnSize=0; 
for(i=0;i

总代码


int* luckyNumbers (int** matrix, int matrixSize, int* matrixColSize, int* returnSize){
int max=INT_MAX;int min=INT_MIN;                     //INT_MAX,INT_MIN分别代表int范围内的最大最小
int minrow[matrixSize];
int maxcol[*matrixColSize];
int *ret=malloc(sizeof(int)*(*matrixColSize));              
// int *ret=malloc(sizeof(int)*matrixSize);         //使用这两种方法都可对数组ret进行内存分配
int i,j;
for(i=0;i=maxcol[i])
        {
            maxcol[i]=matrix[j][i];
        }
    }
}

*returnSize=0; 
for(i=0;i

6.相关练习

<1>.1351. 统计有序矩阵中的负数 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=LA92https://leetcode-cn.com/problems/count-negative-numbers-in-a-sorted-matrix/

 <2>.1572. 矩阵对角线元素的和 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=LA92https://leetcode-cn.com/problems/matrix-diagonal-sum/

<3>.1672. 最富有客户的资产总量 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=LA92https://leetcode-cn.com/problems/richest-customer-wealth/ <4>.766. 托普利茨矩阵 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=LA92https://leetcode-cn.com/problems/toeplitz-matrix/

<5>.1582. 二进制矩阵中的特殊位置 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=LA92https://leetcode-cn.com/problems/special-positions-in-a-binary-matrix/ <6>.463. 岛屿的周长 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=LA92https://leetcode-cn.com/problems/island-perimeter/

 Leetcode---二维数组问题_第2张图片

 

你可能感兴趣的:(力扣算法,算法,c语言,leetcode)