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;
    }
};
相关推荐
逑之10 小时前
C语言笔记10:sizeof和strlen,指针与数组
c语言·笔记·算法
求梦82010 小时前
【力扣hot100题】旋转图像(15)
算法·leetcode·职场和发展
C雨后彩虹14 小时前
任务最优调度
java·数据结构·算法·华为·面试
少林码僧16 小时前
2.31 机器学习神器项目实战:如何在真实项目中应用XGBoost等算法
人工智能·python·算法·机器学习·ai·数据挖掘
钱彬 (Qian Bin)16 小时前
项目实践15—全球证件智能识别系统(切换为Qwen3-VL-8B-Instruct图文多模态大模型)
人工智能·算法·机器学习·多模态·全球证件识别
Niuguangshuo17 小时前
EM算法详解:解密“鸡生蛋“的机器学习困局
算法·机器学习·概率论
a31582380617 小时前
Android 大图显示策略优化显示(一)
android·算法·图片加载·大图片
一条大祥脚17 小时前
26.1.9 轮廓线dp 状压最短路 构造
数据结构·c++·算法
鲨莎分不晴18 小时前
反向传播的数学本质:链式法则与动态规划的完美共舞
算法·动态规划
sonadorje18 小时前
逻辑回归中的条件概率
算法·机器学习·逻辑回归