• 54.Spiral Matrix


    思路
    • 直接模拟程序运行,设置(rowbegin,rowend,colbegin,colend)变量。注意:要判断是否遍历过。 参考
    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            vector<int> res;
            if(matrix.size() == 0) return {};
            int colbegin = 0,colend = matrix[0].size()-1;
            int rowbegin = 0,rowend = matrix.size()-1;
            while(rowbegin <= rowend && colbegin <= colend){
                for(int i = colbegin; i <= colend; i++){
                    res.push_back(matrix[rowbegin][i]);
                }
                rowbegin++;                                         
                //这里也可以加入判断,但实际上没必要
                for(int i = rowbegin; i <= rowend; i++){
                    res.push_back(matrix[i][colend]);
                }
                colend--;
                if(rowbegin <= rowend){                             //判断
                    for(int i = colend; i >= colbegin; i--){
                        res.push_back(matrix[rowend][i]);
                    }
                    rowend--;
                }
                if(colbegin <= colend){
                    for(int i = rowend; i >= rowbegin; i--){
                        res.push_back(matrix[i][colbegin]);
                    }
                    colbegin++;
                }
            }
            return res;
        }
    };
    
    • dfs:待实现
  • 相关阅读:
    EBS R12.2 运行请求出错
    仿ORACLE的TRUNC函数
    EBS职责清单(Responsibility)
    Oracle 11G Client 客户端安装步骤
    UltraIso-写入硬盘映像
    EBS-WIP完工入库
    LeetCode 2 两数相加
    LeetCode 1.两数之和
    装饰器示例
    爬虫day1
  • 原文地址:https://www.cnblogs.com/UniMilky/p/6971389.html
Copyright © 2020-2023  润新知