Matrix Spiral Copy
Function Name: spiralCopy
Description
Write a function that takes a 2D array (matrix) of m x n elements and returns a list of its elements sorted in a spiral order.
The spiral order starts at the top-left corner of the matrix and proceeds in a clockwise direction.
The function should move towards the right on the top row, then proceed downwards along the last column, then move leftward along the bottom row, and finally move up the first column, then continue inwards, following the same pattern until all elements have been visited.
Requirements
- The function 'spiralCopy' must take a single argument - a 2D array (matrix).
- The 2D array may contain integers, characters, or any other data types that can be ordered in a list.
- The function must return a list containing all the elements of the array in the determined spiral order.
- No built-in matrix or array flattening functions may be used; the traversal must be manually coded.
- The function should handle matrices of different dimensions, including edge cases like a single row or a single column matrix.
Examples
If the function is given the following matrix:[ [ 1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]The function should return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].
Sort Colors
Wed Oct 30 2024
Given an array consisting of 'n' elements that represent colors as red, white, or blue, sort the array in-place so that elements of the same color are adjacent, with the colors in the order of red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. You must solve this problem with a one-pass algorithm using only constant space.
Subarray Sum Equals K
Fri Nov 01 2024
Write a function 'subarraySum' that takes an array of integers (nums) and an integer (k), and returns the number of continuous subarrays whose sum equals to k.You may assume that the array and the target integer k will have at least one element.Note that the array can contain both positive and negative integers.