54. Spiral Matrix (Graph)

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) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        result.clear();
        if(matrix.empty()) return result;

        leftPos = 0;
        rightPos = matrix[0].size()-1;
        topPos = 0;
        bottomPos = matrix.size()-1;
        goWider(matrix, true);
        return result;
    }
    void goWider(vector<vector<int>> &matrix, bool direct)
    {
        if(direct)
        {
            for(int i = leftPos; i<= rightPos; i++)
            {
                result.push_back(matrix[topPos][i]);
            }
            topPos++;
            if(topPos > bottomPos) return;
            goDeeper(matrix, true);
        }
        else
        {
            for(int i = rightPos; i>= leftPos; i--)
            {
                result.push_back(matrix[bottomPos][i]);
            }
            bottomPos--;
            if(topPos > bottomPos) return;
            goDeeper(matrix, false);
        }
    }
    void goDeeper(vector<vector<int>> &matrix, bool direct)
    {
        if(direct)
        {
            for(int i = topPos; i<= bottomPos; i++)
            {
                result.push_back(matrix[i][rightPos]);
            }
            rightPos--;
            if(leftPos > rightPos) return;
            goWider(matrix, false);
        }
        else
        {
            for(int i = bottomPos; i>= topPos; i--)
            {
                result.push_back(matrix[i][leftPos]);
            }
            leftPos++;
            if(leftPos > rightPos) return;
            goWider(matrix, true);
        }
    }
private:
    vector<int> result;
    int leftPos;
    int rightPos;
    int topPos;
    int bottomPos;
};
时间: 2024-08-11 01:23:43

54. Spiral Matrix (Graph)的相关文章

[LeetCode]54. Spiral Matrix

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 54. Spiral Matrix &amp; 59. Spiral Matrix II

54. Spiral Matrix [Medium] Description 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:

Leetcode 54:Spiral Matrix 螺旋矩阵

54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1

[Leetcode][Python]54: Spiral Matrix

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 54: Spiral Matrixhttps://leetcode.com/problems/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 foll

leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

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]. 思路:螺旋数组,需

54. Spiral Matrix java solutions

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]. Subscribe to see which c

[leedcode 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]. public class Solution {

54. Spiral Matrix

题目: Given a matrix of m x n elements (m rows, ncolumns), 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 54 Spiral Matrix ----- java

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]. 这道题就是说给定一个数组,然后求一圈一圈的读取数