Baozi Leetcode solution 54: Sprial Matrix

Problem Statement 

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]

Problem link

Video Tutorial

You can find the detailed video tutorial here

Thought Process

It‘s a straight forward implementation question, we can simply just simulate the spiral order and print. You can choose to do it either iteratively (see reference to download the official Leetcode solution) or use recursion.

There is this awesome one line solution from this guy which is pretty insane.

def spiralOrder(self, matrix):
    return matrix and list(matrix.pop(0)) + self.spiralOrder(zip(*matrix)[::-1])

https://leetcode.com/problems/spiral-matrix/discuss/20571/1-liner-in-Python-%2B-Ruby

Solutions

 1 public List<Integer> spiralOrder(int[][] matrix) {
 2     List<Integer> res = new ArrayList<>();
 3
 4     if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
 5         return res;
 6     }
 7
 8     this.printSpiralOrder(0, 0, matrix.length, matrix[0].length, res, matrix);
 9
10     return res;
11 }
12
13 // printSpiralOrder is a poor name, function starts with verb, printSpiralOrder, class is noun, function is verb
14 private void printSpiralOrder(int i, int j, int rowSize, int colSize, List<Integer> res, int[][] matrix) {
15     if (rowSize <= 0 || colSize <= 0) {
16         return;
17     }
18
19     if (rowSize == 1 && colSize == 1) {
20         res.add(matrix[i][j]);
21         return;
22     }
23     if (rowSize == 1) {
24         for (int k = j; k < j + colSize; k++) {
25             res.add(matrix[i][k]);
26         }
27         return;
28     }
29
30     if (colSize == 1) {
31         for (int k = i; k < i + rowSize; k++) {
32             res.add(matrix[k][j]);
33         }
34         return;
35     }
36
37     // do the spiral
38     for (int k = j; k < j + colSize; k++) {
39         res.add(matrix[i][k]);
40     }
41
42     for (int k = i + 1; k < i + rowSize; k++) {
43         res.add(matrix[k][j + colSize - 1]);
44     }
45
46     for (int k = j + colSize - 2; k >= i; k--) {
47         res.add(matrix[i + rowSize - 1][k]);
48     }
49
50     for (int k = i + rowSize - 2; k > i; k--) {   // both the start and end need to be i, j, and also care about length
51         res.add(matrix[k][j]);
52     }
53
54     this.printSpiralOrder(i + 1, j + 1, rowSize - 2, colSize - 2, res, matrix);
55 } 

Simulation using Recursion

Time Complexity: O(M*N) where M, N is row and col of matrix

Space Complexity: O(M*N) since we used list to store the result, where M, N is row and col of matrix

References

原文地址:https://www.cnblogs.com/baozitraining/p/12015938.html

时间: 2024-11-07 23:45:23

Baozi Leetcode solution 54: Sprial Matrix的相关文章

[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

Baozi Leetcode solution 229: Major Element II

Problem Statement Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Outp

Baozi Leetcode solution 1292.&#160;Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Problem Statement Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square. Example 1: Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2

Baozi Leetcode solution 201: Bitwise AND of Numbers Range

Problem Statement Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. Example 1: Input: [5,7] Output: 4 Example 2: Input: [0,1] Output: 0 Problem link Video Tutorial You can find t

Baozi Leetcode solution 135: Candy

Problem Statement There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating

Baozi Leetcode solution 169: Major Element

Problem Statement Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1:

Baozi Leetcode solution 72. Edit Distance

Problem Statement Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1:

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