zoukankan      html  css  js  c++  java
  • 54. Spiral Matrix

    description:

    螺旋输出一个矩阵.
    Note:

    Example:

    Example 1:
    
    Input:
    [
     [ 1, 2, 3 ],
     [ 4, 5, 6 ],
     [ 7, 8, 9 ]
    ]
    Output: [1,2,3,6,9,8,7,4,5]
    
    Example 2:
    
    Input:
    [
      [1, 2, 3, 4],
      [5, 6, 7, 8],
      [9,10,11,12]
    ]
    Output: [1,2,3,4,8,12,11,10,9,5,6,7]
    
    
    

    answer:

    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            if (matrix.empty() || matrix[0].empty()) return {};
            int m = matrix.size(), n = matrix[0].size();
            vector<int> res;
            int up = 0, down = m - 1, left = 0, right = n - 1;
            while (true) {
                for (int j = left; j <= right; ++j) res.push_back(matrix[up][j]);
                if (++up > down) break;
                for (int i = up; i <= down; ++i) res.push_back(matrix[i][right]);
                if (-- right < left) break;
                for (int j = right; j >= left; --j) res.push_back(matrix[down][j]);
                if (--down < up) break;
                for (int i = down; i >= up; --i) res.push_back(matrix[i][left]);
                if (++left > right) break;
            }
            return res;
        }
    };
    

    relative point get√:

    hint :

  • 相关阅读:
    级数问题
    放苹果
    _WIN32_WINNT not defined错误 解决办法
    日期大写
    金额大写转换
    选择屏幕字段不允许直接输入…
    OO面向对象ALV小测试
    判断是否有人在操作某张表,并获取…
    屏幕中设置焦点
    前导零
  • 原文地址:https://www.cnblogs.com/forPrometheus-jun/p/11262696.html
Copyright © 2011-2022 走看看