19.2.8 [LeetCode 54] Spiral Matrix

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]

题意

顺时针螺旋输出数组

题解

 1 class Solution {
 2 public:
 3     vector<int> spiralOrder(vector<vector<int>>& matrix) {
 4         if (matrix.empty())return vector<int>();
 5         int line=matrix.size(),row=matrix[0].size(),n = row*line;
 6         vector<int>ans;
 7         int s = -1, e = row, ss = -1, ee = line;
 8         for (int i = 0; i < (line + 1) / 2; i++) {
 9             s++, e--, ss++, ee--;
10             if (e < s || ee < ss)break;
11             for (int j = s; j <= e; j++)
12                 ans.push_back(matrix[i][j]);
13             for (int j = ss+1; j <= ee; j++)
14                 ans.push_back(matrix[j][e]);
15             if (ss != ee) {
16                 for (int j = e - 1; j >= s; j--)
17                     ans.push_back(matrix[ee][j]);
18             }
19             if (s != e) {
20                 for (int j = ee - 1; j > ss; j--)
21                     ans.push_back(matrix[j][s]);
22             }
23         }
24         return ans;
25     }
26 };

比较容易WA,要处理好特殊情况

原文地址:https://www.cnblogs.com/yalphait/p/10356435.html

时间: 2024-10-10 02:29:01

19.2.8 [LeetCode 54] Spiral Matrix的相关文章

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. 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 螺旋矩阵

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

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

[leetcode]54. Spiral Matrix二维数组螺旋取数

import java.util.ArrayList; import java.util.List; /** * 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

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

【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 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ