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;
    }
};
相关推荐
Morwit23 分钟前
【力扣hot100】 1. 两数之和
数据结构·c++·算法·leetcode·职场和发展
py有趣31 分钟前
力扣热门100题之岛屿的数量(DFS/BFS经典题)
leetcode·深度优先·宽度优先
SpiderPex1 小时前
第十七届蓝桥杯 C++ B组-题目 (最新出炉 )
c++·职场和发展·蓝桥杯
无小道1 小时前
算法——暴力+优化
算法·优化·暴力
Free Tester1 小时前
如何判断 LeakCanary 报告的严重程度
java·jvm·算法
zyq99101_11 小时前
DFS算法实战:经典例题代码解析
python·算法·蓝桥杯·深度优先
智者知已应修善业2 小时前
【51单片机单按键切换广告屏】2023-5-17
c++·经验分享·笔记·算法·51单片机
广州灵眸科技有限公司2 小时前
为RK3588注入澎湃算力:RK1820 AI加速卡完整适配与评测指南
linux·网络·人工智能·物联网·算法
qinian_ztc2 小时前
frida 14.2.18 安装报错解决
算法·leetcode·职场和发展
AI应用实战 | RE2 小时前
012、检索器(Retrievers)核心:从向量库中智能查找信息
人工智能·算法·机器学习·langchain