1424. 对角线遍历 II

文章目录

题意

题目链接

思路

数组左边有值,才可能右边有值;

然后遍历

代码

C++ 复制代码
class Solution {
public:
    vector<int> findDiagonalOrder(vector<vector<int>>& nums) {
        vector<int> ans;
        vector< pair<int, int> > now;
        int i = 0;
        while (i < nums.size() || !now.empty()) {
            vector<pair<int, int> > tmp;
            if (i < nums.size())
            {
                ans.push_back(nums[i][0]);
                tmp.push_back(make_pair(i, 0));
            }
            i++;
            for (auto &index:now)
            {
                int x = index.first;
                int y = index.second + 1;
                if (x < nums.size() && y < nums[x].size())
                {
                    tmp.push_back(make_pair(x, y));
                    ans.push_back(nums[x][y]);
                }
            }
            now = tmp;
        }
        return ans;
    }
};
相关推荐
数模竞赛Paid answer11 小时前
2026年中青杯数学建模A题数学建模论文智能评估系统与多智能体优化方法求解全过程论文及程序
算法·数学建模·中青杯
银-豆豆11 小时前
数据结构与算法-动态规划、回溯与贪心
算法·动态规划
JL1511 小时前
面试项目被问到自闭-如何把课设包装成亮眼项目
面试·职场和发展
想吃火锅100513 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
Nil20813 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
依然鸣14 小时前
PTA团体程序设计天梯赛L1真题讲解L1-077-080
开发语言·c++·算法·深度优先·pat考试·图论
qeen8717 小时前
【数据结构】自平衡二叉搜索树各种旋转算法原理解析及AVL树的C++实现
数据结构·c++·算法
lucas_AI17 小时前
1.2B 小模型赢过 235B 大模型:NaviDC-OCR 把文档解析卷明白了
人工智能·深度学习·算法
冻柠檬飞冰走茶18 小时前
PTA基础编程题目集 7-35有理数均值(C++语言实现)
开发语言·数据结构·c++·算法·均值算法