Matrix Spiral Traversal
Function Name: spiralOrder
Description
Write a function that takes an m x n 2D array (matrix) and returns a list of its elements in spiral order. Spiral order starts at the top-left corner of the matrix, proceeds to the right, and follows a clockwise pattern inward.
The function should account for both rectangular and square matrices, and it should handle empty matrices as well, returning an empty list for the latter.
Requirements
- The function should be named 'spiralOrder'.
- It should take a single argument, matrix, which is a list of lists representing a 2D array.
- Do not use any built-in language methods to directly perform the spiral traversal.
- The function should return a list of elements representing the spiral order of traversal.
- Solve the problem iteratively rather than using recursion to manage space complexity.
- Optimize for readability and consider edge cases, such as an empty matrix or a matrix with only one row or column.
Examples
Example 1:Input: spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]])Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]Example 2:Input: spiralOrder([[1], [2], [3]])Output: [1, 2, 3]Example 3:Input: spiralOrder([])Output: []
Links
Balance Brackets
Mon Sep 23 2024
Write a function called balanceBrackets that takes a single string parameter consisting of different brackets (e.g., '(', ')', '[', ']', '{', '}') and returns a boolean indicating whether the brackets in the string are balanced. Brackets are considered to be balanced if every opening bracket has a corresponding closing bracket and the pairs of brackets are properly nested.
String Permutations
Wed Sep 25 2024
In this challenge, you are required to write a function named 'generateStringPermutations' that takes a single string as an input parameter and returns all possible permutations of the characters in the string. A permutation is a re-arrangement of the elements of an ordered list. Each permutation should be unique, meaning if the string contains duplicate characters, permutations that are identical should be returned only once. The order of permutations in the returned collection does not matter.