Tag: matrix
Primer – Example – Spiral
Given a m x n
matrix, where m
is the number of rows and n
is the number of columns, return all elements of the matrix in spiral order. Solution is here, password is array-example-spiral
Example
Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Return: [1, 2, 3, 6, 9, 8, 7, 4, 5]
You can check your code here.
Here is a YouTube video with the solution approach:
Templates
C
/** Return an integer array after spiral traversing a matrix * * @returns integer array pointer * @param A: 2D integer array * @params m, n: Dimensions of the matrix * @param len: length of the output array (technically an outut) */ int* spiralOrder(const int** A, int m, int n, int *len) { // TODO: Your sollution here }
C++
/** Return an integer array after spiral traversing a matrix * * @returns integer vector * @param A: vector of integer vectors */ vector<int> spiralOrder(const vector<vector<int> > &A) { // TODO: Your code here }
Java
public class spiral { /** Return an integer array after spiral traversing a matrix * * @returns ArrayList<Integer> * @param A: 2D integer array (List<ArrayList<Integer>>) */ public static ArrayList<Integer> spiralOrder(final List<ArrayList<Integer>> A) { // TODO: Your code here } };
Python
def spiralOrder(A): """Return an integer array after spiral traversing a matrix @returns integer vector @param A: vector of integer vectors """ pass