LeetCode: Spiral Matrix [058]

【题目】

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].

【题意】

螺旋输出MxN矩阵 【注意是MxN】

【思路】

Rotate Image思路相同,从外向外逐层输出即可

注意处理两种特殊情况:外圈基本上都是由四边组成的举行,而矩阵中心的矩形很可能只是单行或者单列

1. 单行

2. 单列

【代码】

class Solution {
public:
    void output(vector<int>&result, vector<vector<int> >&matrix, int i, int j, int rows, int cols){

        if(rows<1 || cols<1)return;
        if(rows == 1){
			for(int y=j; y<j+cols; y++){
				result.push_back(matrix[i][y]);
			}
            return;
        }
		if(cols==1){
			for(int x=i; x<i+rows; x++){
				result.push_back(matrix[x][j]);
			}
			return;
		}

        //输出上边行
        int x=i, y=j;
        while(y<j+cols){
            result.push_back(matrix[x][y]);
            y++;
        }
        //输出右边行
        x=i+1; y=j+cols-1;
        while(x<i+rows){
            result.push_back(matrix[x][y]);
            x++;
        }
        //输出下边行
        x=i+rows-1; y=j+cols-2;
        while(y>=j){
            result.push_back(matrix[x][y]);
            y--;
        }
        //输出左边行
        x=i+rows-2; y=j;
        while(x>i){
            result.push_back(matrix[x][y]);
            x--;
        }

        //输出内圈
        output(result, matrix, i+1, j+1, rows-2, cols-2);
    }
    vector<int> spiralOrder(vector<vector<int> > &matrix) {
        vector<int>result;
		int rows=matrix.size();
		if(rows==0)return result;
		int cols=matrix[0].size();
        output(result,matrix, 0, 0,rows, cols);
        return result;
    }
};

LeetCode: Spiral Matrix [058]

时间: 2024-12-28 21:51:34

LeetCode: Spiral Matrix [058]的相关文章

LeetCode: Spiral Matrix II [058]

[题目] 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 ] ] [题意] 给定整数n, 将1,2,3...nxn个数按螺旋旋转的方式填入nxn的矩

LeetCode: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]. 打印螺旋矩阵 逐个

LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

Spiral Matrix IIGiven 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 ]] SOLUTION 1: 还是与上一题Spiral Matrix类似

leetcode Spiral Matrix II

题目:是Spiral Matrix相关的的.这题的意思是给定一个n,那么在n*n的矩阵里按照循环记录将1,2,3,..., n*n.如下如果给定3,那么: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]一开始我想是不是有数学公式直接下标对应的,那直接遍历输出就可以了.但是推了一会,没有什么好的头绪.于是就分情况讨论了,和Spiral Matrix一样的分情况,只是这里的分情况是记录在数组里面.利用上,下,左,右,分别记录每次循环可以到达的四个边界,我们每次就输

[LeetCode]Spiral Matrix 54

54.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]. 好久没做题了,

LeetCode: Spiral Matrix 解题报告

Spiral MatrixGiven 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]. Solution 1: 使

LeetCode——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]. 原题链接:https://oj.leetcod

LeetCode——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 ] ] 原题链接:https://oj.leetcode.com/problems/spiral-m

[Leetcode] 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]. 题意:以螺旋的方式,顺时针访问数组. 思路:按照遍