当前位置: 首页 > article >正文

一通瞎写居然击败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~,结果是我没想到…


http://www.kler.cn/news/357428.html

相关文章:

  • 【学术会议-6】激发灵感-计算机科学与技术学术会议邀您参与,共享学术盛宴,塑造明天的科技梦想!
  • 计算生物学与生物信息学漫谈-2-测序深度/读长质量和Fasta处理
  • Cornerstone3D Tools对影像进行交互(中篇)-注释类工具使用
  • React JSX 使用条件语句渲染UI的两种写法
  • SS34二极管
  • WPF -- LiveCharts的使用和源码
  • uniapp 单表、多级动态表单添加validateFunction自定义规则
  • Mybatis操作
  • Vue中v-bind对样式控制的增强—(详解v-bind操作class以及操作style属性,附有案例+代码)
  • Golang | Leetcode Golang题解之第479题最大回文数乘积
  • 测试用例评审流程优化
  • 智能时代摩托车一键启动无钥匙进入感受科技前线
  • 《计算机视觉》—— 基于dlib库的人检检测
  • C#从零开始学习(类型和引用)(4)
  • ARM 中断控制器 GIC-V2
  • Unity3D ScrollView 滚动视图组件详解及代码实现
  • 世界脑力锦标赛资料,最强大脑教程
  • 鸿蒙网络编程系列23-实现一个基于鸿蒙API的HTTP服务器
  • git的学习使用(搭建本地仓库,创建本地仓库,配置本地仓库)(附带Ubuntu云服务器git安装流程)
  • QShortcut实现快捷键