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;
    }
};
相关推荐
西望云天29 分钟前
The 2023 ICPC Asia Shenyang Regional Contest(2023沈阳区域赛CEJK)
数据结构·算法·icpc
zh_xuan1 小时前
LeeCode92. 反转链表II
数据结构·算法·链表·leecode
2401_841495641 小时前
【数据结构】汉诺塔问题
java·数据结构·c++·python·算法·递归·
Q741_1472 小时前
C++ 位运算 高频面试考点 力扣137. 只出现一次的数字 II 题解 每日一题
c++·算法·leetcode·面试·位运算
天特肿瘤电场研究所2 小时前
专业的肿瘤电场疗法厂家
算法
DASXSDW2 小时前
NET性能优化-使用RecyclableBuffer取代RecyclableMemoryStream
java·算法·性能优化
kfepiza2 小时前
CAS (Compare and Swap) 笔记251007
java·算法
墨染点香3 小时前
LeetCode 刷题【103. 二叉树的锯齿形层序遍历、104. 二叉树的最大深度、105. 从前序与中序遍历序列构造二叉树】
算法·leetcode·职场和发展
啊我不会诶3 小时前
23ICPC澳门站补题
算法·深度优先·图论
Brookty4 小时前
【算法】二分查找(一)朴素二分
java·学习·算法·leetcode·二分查找