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

    Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

    Example:

    Input: 3
    Output:
    [
     [ 1, 2, 3 ],
     [ 8, 9, 4 ],
     [ 7, 6, 5 ]
    ]
    class Solution {
    public:
        vector<vector<int>> generateMatrix(int n) {
    
            vector<vector<int>> res(n,vector<int>(n,0));
            if (n == 0) return res;
            int i = 1;
            int rowS = 0,rowE = n - 1,colS = 0,colE = n -1;
            while(i <= n * n)
            {
                for (int j = colS;j <= colE;++j)
                {
                    res[rowS][j] = i++;
                }
                rowS++;
                
                for (int j = rowS;j <= rowE;++j)
                {
                    res[j][colE] = i++;
                }
                colE--;
                
                for (int j = colE;j >= colS;--j)
                {
                    res[rowE][j] = i++;
                }
                rowE--;
                
                for (int j = rowE;j >= rowS;--j)
                {
                    res[j][colS] = i++;
                }
                colS++;
            }
            return res;
        }
    };

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

    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]
    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            int m = matrix.size(), n = m ? matrix[0].size() : 0, u = 0, d = m - 1, l = 0, r = n - 1, p = 0;
            vector<int> order(m * n);
            while (u <= d && l <= r) {
                for (int col = l; col <= r; col++) {
                    order[p++] = matrix[u][col];
                }
                if (++u > d) {
                    break;
                }
                for (int row = u; row <= d; row++) {
                    order[p++] = matrix[row][r];
                }
                if (--r < l) {
                    break;
                }
                for (int col = r; col >= l; col--) {
                    order[p++] = matrix[d][col];
                }
                if (--d < u) {
                    break;
                }
                for (int row = d; row >= u; row--) {
                    order[p++] = matrix[row][l];
                }
                if (l++ > r) {
                    break;
                }
            }
            return order;
        }
    };
  • 相关阅读:
    IPUtil
    MD5Util
    MyBatis环境配置及入门
    淘宝主页(静态页面)第3天
    淘宝主页(静态页面)第二天
    淘宝主页(静态页面)第1天
    力扣20 有效的括号
    力扣1 two sum
    JAVA可变参数
    JAVA环形队列
  • 原文地址:https://www.cnblogs.com/qiang-wei/p/11985786.html
Copyright © 2011-2022 走看看