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;
    }
};
相关推荐
一去不复返的通信er2 分钟前
力扣-图论-岛屿数量
算法·leetcode·图论
考研保研资料分享2 小时前
985工科跨保上财应用统计:数学笔试、匿名面试与机试复盘
面试·职场和发展
程序 代码狂人3 小时前
切片表与分区表的区别(数据开发面试版)
java·大数据·开发语言·数据仓库·算法
HDU-Dade4 小时前
【校招笔试真题】简要介绍 Self-Attention 的前沿算法
人工智能·算法
Xin7704 小时前
LeetCode 79. 单词搜索(DFS 回溯法)
leetcode
小郎君6586 小时前
Vue keep-alive 切标签后滚动位置回顶?因为 onDeactivated 里读到的 scrollTop 是 0
算法
邵奈一6 小时前
GPT-6.1 Terra速度是Sol的3倍,价格减半
算法
RaaS1006 小时前
Node.js Brotli RCE 漏洞自查指南:fetch、undici、axios 都在影响面,三分钟确认你的版本
人工智能·深度学习·算法
shehuiyuelaiyuehao6 小时前
算法56,重排链表
数据结构·算法·链表