Closed jsartisan closed 2 hours ago
difficulty: medium title: Spiral Matrix type: question template: typescript tags: javascript, blind75, matrix
Given an m x n matrix, return all elements in spiral order (clockwise from outside to inside).
Rules:
Constraints:
Examples:
// Example 1: 2x2 matrix console.log(spiralOrder([[1,2],[3,4]])); // Output: [1,2,4,3] // Example 2: 3x3 matrix console.log(spiralOrder([[1,2,3],[4,5,6],[7,8,9]])); // Output: [1,2,3,6,9,8,7,4,5] // Example 3: 3x4 matrix console.log(spiralOrder([[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]
index.ts
export function spiralOrder(matrix: number[][]): number[] { }
index.test.ts
import { spiralOrder } from './index'; describe('spiralOrder', () => { test('Example 1: 2x2 matrix', () => { expect(spiralOrder([[1,2],[3,4]])).toEqual([1,2,4,3]); }); test('Example 2: 3x3 matrix', () => { expect(spiralOrder([[1,2,3],[4,5,6],[7,8,9]])) .toEqual([1,2,3,6,9,8,7,4,5]); }); test('Example 3: 3x4 matrix', () => { expect(spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12]])) .toEqual([1,2,3,4,8,12,11,10,9,5,6,7]); }); test('1x1 matrix', () => { expect(spiralOrder([[7]])).toEqual([7]); }); test('1xn matrix', () => { expect(spiralOrder([[1,2,3]])).toEqual([1,2,3]); }); test('nx1 matrix', () => { expect(spiralOrder([[1],[2],[3]])).toEqual([1,2,3]); }); test('4x4 matrix', () => { expect(spiralOrder([ [1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] ])).toEqual([1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]); }); test('Matrix with negative numbers', () => { expect(spiralOrder([[-1,-2],[-3,-4]])).toEqual([-1,-2,-4,-3]); }); test('2x3 matrix', () => { expect(spiralOrder([[1,2,3],[4,5,6]])).toEqual([1,2,3,6,5,4]); }); test('3x2 matrix', () => { expect(spiralOrder([[1,2],[3,4],[5,6]])).toEqual([1,2,4,6,5,3]); }); });
Info
Question
Given an m x n matrix, return all elements in spiral order (clockwise from outside to inside).
Rules:
Constraints:
Examples:
Template
index.ts
index.test.ts