Java Clean Solution | 08/08/2024
Complexity\n- Time complexity:O((row+col)4)\n Add your time complexity here, e.g. O(n) \n\n- Space complexityO((row+col)2)\n Add your space complexity here, e.g
# Complexity\n- Time complexity:$$O((row+col)*4)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity$$O((row+col)*2)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    int[][] dir = new int[][] { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };\n\n    public int[][] spiralMatrixIII(int rows, int cols,\n            int rStart, int cStart) {\n\n        int[][] res = new int[rows * cols][2];\n        int idx = 0;\n        res[idx++] = new int[] { rStart, cStart };\n\n        int idx_d = 0, len = 0;\n        while (idx < rows * cols) {\n            if (idx_d == 0 || idx_d == 2) {\n                len++;\n            }\n\n            for (int i = 0; i < len; i++) {\n                rStart += dir[idx_d][0];\n                cStart += dir[idx_d][1];\n                if (rStart >= 0 && rStart < rows\n                        && cStart >= 0 && cStart < cols) {\n                    res[idx++] = new int[] { rStart, cStart };\n                }\n            }\n            idx_d = (idx_d + 1) % 4;\n        }\n        return res;\n    }\n}\n```
['Array', 'Matrix', 'Simulation', 'Java']