• Spiral Matrix I II


    Spiral Matrix

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

    For example,
    Given the following matrix:

    [
     [ 1, 2, 3 ],
     [ 4, 5, 6 ],
     [ 7, 8, 9 ]
    ]
    

    You should return [1,2,3,6,9,8,7,4,5].

    题解:

    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int> > &matrix) {
            vector<int> res;
            int row = matrix.size();
            if(row==0)
                return res;
            int col = matrix[0].size();
            int k = (1+min(row, col))/2;
            int row0 = 0, col0 = 0;
            int i=0, j=0;
            while(k--) {
                if((row-row0)==1)
                    for(j=col0;j<col;j++)
                        res.push_back(matrix[row0][j]);
                else if((col-col0)==1)
                    for(i=row0;i<row;i++)
                        res.push_back(matrix[i][col-1]);
                else {
                    for(j=col0;j<col;j++)
                         res.push_back(matrix[row0][j]);
                    res.pop_back();
                    for(i=row0;i<row;i++)
                         res.push_back(matrix[i][col-1]);
                    res.pop_back();
                    for(j=col-1;j>=col0;j--)
                         res.push_back(matrix[row-1][j]);
                    res.pop_back();
                    for(i=row-1;i>=row0;i--)
                         res.push_back(matrix[i][col0]);
                    res.pop_back();
                    row0++;col0++;
                    row--;col--;
                }
            }
            return res;
        }
    };

    Spiral Matrix II

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

    For example,
    Given n = 3,

    You should return the following matrix:

    [
     [ 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));
            int start = 0;
            int end = n-1;
            int i=0, j=0;
            int num = 1;
            while(start<end) {
                for(j=start;j<end;j++)
                    res[start][j] = num++;
                for(i=start;i<end;i++)
                    res[i][end] = num++;
                for(j=end;j>start;j--)
                    res[end][j] = num++;
                for(i=end;i>start;i--)
                    res[i][start] = num++;
                start++;end--;
            }
            if(start==end)
                res[end][end] = num;
            return res;
        }
    };
  • 相关阅读:
    ES6中的class的详解
    JavaSrcipt中字符串和数组容易搞混的操作
    ES6中的数据结构Map的理解和描述
    ES6中新增数据结构Set的理解和用法详情描述
    ES6中的Promise的用法总结
    JS中的async/await的用法和理解
    字节跳动、拼多多前端面经
    前端项目优化 -Web 开发常用优化方案、Vue & React 项目优化
    记几个 DOM 操作技巧
    JavaScript 面试题
  • 原文地址:https://www.cnblogs.com/jiasaidongqi/p/4172854.html
Copyright © 2020-2023  润新知