一通瞎写居然击败100%【力扣】【498-对角线遍历】【数组-C语言】
题目:力扣-498
给你一个大小为 m x n 的矩阵 mat ,请以对角线遍历的顺序,用一个数组返回这个矩阵中的所有元素。
代码如下:
int* findDiagonalOrder(int** mat, int matSize, int* matColSize, int* returnSize){int direct[2][2]={{-1,1},{1,-1}};//direct[0]表示从下网上,direc[1]表示从上往下int m=matSize;int n=*matColSize;int *retArr =(int*)malloc(sizeof(int)*m*n);int total = 0;int row=0;int col=0;int directIndex=0;while(total<m*n){retArr[total]=mat[row][col];int nextRow = row + direct[directIndex][0];int nextCol = col + direct[directIndex][1];if(nextRow<0||nextCol>=n||nextRow>=m||nextCol<0){if(nextRow<0&&nextCol<n&&directIndex==0){//上边越界col+=1;}else if(nextCol<0&&nextRow<m&&directIndex==1){//左边越界row+=1;}else if(nextRow<0&&nextCol==n&&directIndex==0){//右上顶角越界row+=1;}else if(nextCol<0&&nextRow==m&&directIndex==1){//左下顶角越界col+=1;}else if(nextRow==m&&nextCol>=0&&directIndex==1){//底部越界col+=1;}else if(nextCol==n&&nextRow>=0&&directIndex==0){//右边越界row+=1;}directIndex=directIndex==0?1:0;}else{row+=direct[directIndex][0];col+=direct[directIndex][1];}total++;}(*returnSize)=m*n;return retArr;
}
写的时候感觉就是一坨 shi~,结果是我没想到…