💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\n\n\n\n\njavascript []\n//JavaScript Code\n/**\n * @param {number} rows\n * @param {number} cols\n * @param {number} rStart\n * @param {number} cS
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/51b1acbf-2c53-4c43-814b-60ba92113a8e_1723108734.8664858.jpeg)\n![Screenshot 2024-08-08 143337.png](https://assets.leetcode.com/users/images/6e71e8da-4d28-43e5-bf68-ebfc65289940_1723108743.7015176.png)\n![Screenshot 2024-08-08 143357.png](https://assets.leetcode.com/users/images/b85a8eac-71dc-4007-af74-abe1d1a59e03_1723108761.3428636.png)\n![Screenshot 2024-08-08 144127.png](https://assets.leetcode.com/users/images/dc434b42-690f-4a2a-bbb4-ec998bb4edb8_1723108768.0345154.png)\n![Screenshot 2024-08-08 144335.png](https://assets.leetcode.com/users/images/4e04a2a6-8879-4bf6-8359-cd3d55177c35_1723108774.39793.png)\n![Screenshot 2024-08-08 144600.png](https://assets.leetcode.com/users/images/c51d7af2-a6ba-4fee-a13c-4f63a88e66be_1723108781.411554.png)\n```javascript []\n//JavaScript Code\n/**\n * @param {number} rows\n * @param {number} cols\n * @param {number} rStart\n * @param {number} cStart\n * @return {number[][]}\n */\nvar spiralMatrixIII = function(rows, cols, rStart, cStart) {\n    const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]; // East, South, West, North\n    const result = [[rStart, cStart]];\n    let steps = 0, d = 0;\n    \n    while (result.length < rows * cols) {\n        if (d % 2 === 0) steps++;\n        \n        for (let i = 0; i < steps; i++) {\n            rStart += directions[d][0];\n            cStart += directions[d][1];\n            \n            if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols)\n                result.push([rStart, cStart]);\n            \n            if (result.length === rows * cols) return result;\n        }\n        \n        d = (d + 1) % 4;\n    }\n    \n    return result;\n};\n```\n```C++ []\n//C++ Code\nclass Solution {\npublic:\n    vector<vector<int>> offset = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n\n        int cells = rows * cols;\n        int offsetVal = 0;\n        vector<vector<int>> ans;\n        int i = rStart, j = cStart, len = 0;\n        ans.push_back({rStart, cStart});\n\n        while (ans.size() < cells) {\n            if (offsetVal == 0 || offsetVal == 2)\n                len++;\n            for (int k = 0; k < len; k++) {\n                i += offset[offsetVal][0];\n                j += offset[offsetVal][1];\n                if (i >= 0 && i < rows && j >= 0 && j < cols)\n                    ans.push_back({i, j});\n            }\n            offsetVal = (++offsetVal) % 4;\n        }\n\n        return ans;\n    }\n};\n```\n```Python3 []\n#Python3 Code\nclass Solution:\n    def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int) -> List[List[int]]:\n        l = rows * cols\n        res = [[rStart,cStart]]\n        step = 1\n        while(len(res) < l):\n            for i in range(step):\n                cStart += 1\n                if 0<=rStart< rows and 0<=cStart < cols:\n                    res.append([rStart,cStart])\n            for i in range(step):\n                rStart += 1\n                if 0<=rStart< rows and 0<=cStart < cols:\n                    res.append([rStart,cStart])\n            step += 1\n            for i in range(step):\n                cStart -= 1\n                if 0<=rStart< rows and 0<=cStart < cols:\n                    res.append([rStart,cStart])\n            for i in range(step):\n                rStart -= 1\n                if 0<=rStart< rows and 0<=cStart < cols:\n                    res.append([rStart,cStart])\n            step+=1\n        return res\n            \n            \n            \n        \n```\n```Java []\n//Java Code\nclass Solution {\n    public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        int[][] Indexmatrix = new int[rows * cols][2];\n        int Index = 1;\n        int rowvalue = 1;\n        int increasingvalue = 1;\n        Indexmatrix[0][0] = rStart;\n        Indexmatrix[0][1] = cStart;\n        while (Index < rows * cols) {\n            for (int i = 1; i <= increasingvalue; i++) {\n                cStart++;\n                if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {\n                    Indexmatrix[rowvalue][0] = rStart;\n                    Indexmatrix[rowvalue][1] = cStart;\n                    rowvalue++;\n                    Index++;\n                }\n            }\n            for (int i = 1; i <= increasingvalue; i++) {\n                rStart++;\n                if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {\n                    Indexmatrix[rowvalue][0] = rStart;\n                    Indexmatrix[rowvalue][1] = cStart;\n                    rowvalue++;\n                    Index++;\n                }\n            }\n            increasingvalue++;\n            for (int i = 1; i <= increasingvalue; i++) {\n                cStart--;\n                if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {\n                    Indexmatrix[rowvalue][0] = rStart;\n                    Indexmatrix[rowvalue][1] = cStart;\n                    rowvalue++;\n                    Index++;\n                }\n            }\n            for (int i = 1; i <= increasingvalue; i++) {\n                rStart--;\n                if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {\n                    Indexmatrix[rowvalue][0] = rStart;\n                    Indexmatrix[rowvalue][1] = cStart;\n                    rowvalue++;\n                    Index++;\n                }\n            }\n            increasingvalue++;\n        }\n        return Indexmatrix;\n    }\n}\n```\n```C []\n//C Code\n/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume\n * caller calls free().\n */\nint** spiralMatrixIII(int rows, int cols, int rStart, int cStart,\n                      int* returnSize, int** returnColumnSizes) {\n    // Directions array representing movements in order: right, down, left, up\n    int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\n    // Calculate the total number of cells\n    int totalCells = rows * cols;\n\n    // Allocate memory for the result array\n    int** result = (int**)malloc(totalCells * sizeof(int*));\n    for (int i = 0; i < totalCells; i++) {\n        result[i] = (int*)malloc(2 * sizeof(int));\n    }\n\n    // Allocate memory for returnColumnSizes and initialize it\n    *returnColumnSizes = (int*)malloc(totalCells * sizeof(int));\n    for (int i = 0; i < totalCells; i++) {\n        (*returnColumnSizes)[i] = 2;\n    }\n\n    // Initialize the starting position and direction\n    int r = rStart, c = cStart, directionIndex = 0;\n    int steps = 1;     // Number of steps to move in the current direction\n    int cellCount = 0; // Number of cells visited\n\n    // Loop until we have visited all cells\n    while (cellCount < totalCells) {\n        // Loop twice for each step increment\n        for (int i = 0; i < 2; i++) {\n            // Move in the current direction for the specified number of steps\n            for (int j = 0; j < steps; j++) {\n                // If the current cell is within the grid boundaries, add it to\n                // the result\n                if (r >= 0 && r < rows && c >= 0 && c < cols) {\n                    result[cellCount][0] = r;\n                    result[cellCount][1] = c;\n                    cellCount++;\n                }\n                // Move to the next cell in the current direction\n                r += directions[directionIndex][0];\n                c += directions[directionIndex][1];\n            }\n            // Change direction (right -> down -> left -> up)\n            directionIndex = (directionIndex + 1) % 4;\n        }\n        // Increase the number of steps after two direction changes\n        steps++;\n    }\n\n    // Set the return size\n    *returnSize = totalCells;\n    return result;\n}\n```\n```Python []\n#Python Code\nclass Solution(object):\n    def spiralMatrixIII(self, rows, cols, rStart, cStart):\n        """\n        :type rows: int\n        :type cols: int\n        :type rStart: int\n        :type cStart: int\n        :rtype: List[List[int]]\n        """\n        off_set = [[0, 1], [1, 0], [0, -1], [-1, 0]]\n        cells = rows * cols\n        off_set_val = 0\n        ans = []\n        i , j, length = rStart, cStart, 0\n        ans.append([rStart, cStart])\n        while(len(ans) < cells):\n            if off_set_val == 0 or off_set_val == 2:\n                length += 1\n            for k in range(length):\n                i += off_set[off_set_val][0]\n                j += off_set[off_set_val][1]\n                if i >= 0 and i < rows and j >= 0 and j < cols:\n                    ans.append([i, j])\n            off_set_val += 1\n            off_set_val %= 4\n        return ans\n```\n```C# []\n//C# code\npublic class Solution {\n    public int[][] SpiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n\t\tint cells = rows * cols;\n        int[][] result = new int[cells][];\n\t\tint[][] direction = new int[][] { new int[] { 0, 1 }, new int[] { 1, 0 }, new int[] { 0, -1 }, new int[] { -1, 0 } };\n\n\t\tint step = 1;\n\t\tint count = 0;\n\t\tint turn = 0;\n\t\tint row = rStart;\n\t\tint col = cStart;\n\t\twhile (count < cells){\n\t\t\tfor (int move = 0; move < step; move++){\n\t\t\t\tif (row >= 0 && row < rows && col >= 0 && col < cols)\n\t\t\t\t\tresult[count++] = new int[]{row, col};\n\t\t\n\t\t\t\trow += direction[turn][0];\n\t\t\t\tcol += direction[turn][1];\n\t\t\t}\n\t\t\tturn = (turn + 1) % 4;\n\t\t\tif (turn == 0 || turn == 2)\n\t\t\t\tstep++;\n\t\t}\n\t\treturn result;\n    }\n}\n```\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n![th.jpeg](https://assets.leetcode.com/users/images/8f3d0846-7198-488d-a3cb-23bc7c4a297e_1723109061.9110816.jpeg)\n
['Array', 'C', 'Matrix', 'Simulation', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']