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

这是一个螺旋排序的问题

这里遇到一个比较巧妙的方法,就是利用方向矩阵

class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
    vector<int> result;
    if(matrix.empty())
    {
        return result;
    }
    int m = matrix.size();
    int n = matrix[0].size();
    int direct = 0; //1:right,2:down,3:left,4:up
    int x[4] = {1,0,-1,0};
    int y[4] = {0,1,0,-1};

    int visitedRow = 0;
    int visitedCol = 0;
    int candidant = 0;
    int startx = 0;
    int starty = 0;
    int step = 0;
    while(true)
    {
        if(x[direct] == 0)//<表示做列的操作,关心的是行的数量
        {
            candidant = m-visitedRow;
        }
        else
        {
            candidant = n-visitedCol;//<做的是行的操作,关心的是列的数量
        }

        if(candidant <= 0) break;
        result.push_back(matrix[startx][starty]);
        step++;
        if(step == candidant) //<表示这个方向走完了
        {
            visitedRow += x[direct]==0? 0 : 1;
            visitedCol += y[direct]==0? 0 : 1;
            step = 0;
            direct++;
            direct = direct%4;
        }
        startx += y[direct]; //<x的位置和y方向的操作有关,注意这里的一个反向的操作
        starty += x[direct]; //<下一步的跳转过程
    }

    return result;
}
};

下面的方法就是比较简单的就是利用一个递归,每一次扫描一圈:

class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
    vector<int> result;
    if(matrix.empty())
    {
        return result;
    }
    int m = matrix.size();
    int n = matrix[0].size();

    //<通过观察,可以发现一定的循环规律
    SprialOrder(matrix,0,0,m,n,result);
    return result;
}
void SprialOrder(vector<vector<int> > &matrix,int startm,int startn,int m,int n,vector<int>& result)
{
    if(m <= 0 || n <= 0)
    {
        return;
    }
    for(int i = 0; i < n; i++)
    {
        result.push_back(matrix[startm][startn+i]);
    }
    for(int j = 1; j < m; j++)
    {
        result.push_back(matrix[startm+j][startn+n-1]);
    }
    for(int i = n-2; (i >= 0 && m >= 2);i--)
    {
        result.push_back(matrix[startm+m-1][startn+i]);
    }
    for(int j = m-3; j>=0; j--)
    {
        result.push_back(matrix[startm+j][startn]);
    }
    SprialOrder(matrix,startm+1,startn+1,m-2,n-2,result);
    return;
}
};
时间: 2024-10-11 04:16:03

LeetCode—*Spiral Matrix问题,主要是用到了方向矩阵,很创意的相关文章

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 [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矩阵 [注意

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