Simulate The Spiral Traversal | Java | C++ | [Video Solution]
Intuition, approach, and complexity discussed in video solution in detail\nhttps://youtu.be/ZU62vPZMfUI\n# Code\nJava\n\nclass Solution {\n    public int[][] sp
# Intuition, approach, and complexity discussed in video solution in detail\nhttps://youtu.be/ZU62vPZMfUI\n# Code\nJava\n```\nclass Solution {\n    public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        int totalCells = rows * cols;\n        int leftLimit = cStart, rightLimit = cStart;\n        int upLimit = rStart, downLimit = rStart;\n        int visitedOrder[][] = new int[totalCells][2];\n        int visitedIndx = 0;\n        int currR = rStart, currC = cStart;\n        visitedOrder[visitedIndx++] = new int[]{currR, currC};\n        while(visitedIndx < totalCells){\n           while(currC < rightLimit + 1){\n              currC++;\n              if(isInGrid(currR, currC, rows, cols) && visitedIndx < totalCells){\n                visitedOrder[visitedIndx++] = new int[]{currR, currC};\n              }\n           }\n           rightLimit++;\n           while(currR < downLimit + 1){\n            currR++;\n            if(isInGrid(currR, currC, rows, cols) && visitedIndx < totalCells){\n                visitedOrder[visitedIndx++] = new int[]{currR, currC};\n            }\n           }\n           downLimit++;\n           while(currC > leftLimit-1){\n              currC--;\n              if(isInGrid(currR, currC, rows, cols) && visitedIndx < totalCells){\n                \n                visitedOrder[visitedIndx++] = new int[]{currR, currC};\n              }\n           }\n           leftLimit--;\n           while(currR > upLimit-1){\n              currR--;\n              if(isInGrid(currR, currC, rows, cols) && visitedIndx < totalCells){\n                \n                visitedOrder[visitedIndx++] = new int[]{currR, currC};\n              }\n           }\n           upLimit--;\n        }\n        return visitedOrder;\n    }\n    private boolean isInGrid(int r, int c, int rows, int cols){\n        return r > -1 && c > -1 && r < rows && c < cols;\n    }\n}\n```\nC++\n```\nclass Solution {\npublic:\n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        int totalCells = rows * cols;\n        int leftLimit = cStart, rightLimit = cStart;\n        int upLimit = rStart, downLimit = rStart;\n        vector<vector<int>> visitedOrder(totalCells, vector<int>(2));\n        int visitedIndx = 0;\n        int currR = rStart, currC = cStart;\n        visitedOrder[visitedIndx++] = {currR, currC};\n        while(visitedIndx < totalCells) {\n            while(currC < rightLimit + 1) {\n                currC++;\n                if(isInGrid(currR, currC, rows, cols) && visitedIndx < totalCells) {\n                    visitedOrder[visitedIndx++] = {currR, currC};\n                }\n            }\n            rightLimit++;\n            while(currR < downLimit + 1) {\n                currR++;\n                if(isInGrid(currR, currC, rows, cols) && visitedIndx < totalCells) {\n                    visitedOrder[visitedIndx++] = {currR, currC};\n                }\n            }\n            downLimit++;\n            while(currC > leftLimit - 1) {\n                currC--;\n                if(isInGrid(currR, currC, rows, cols) && visitedIndx < totalCells) {\n                    visitedOrder[visitedIndx++] = {currR, currC};\n                }\n            }\n            leftLimit--;\n            while(currR > upLimit - 1) {\n                currR--;\n                if(isInGrid(currR, currC, rows, cols) && visitedIndx < totalCells) {\n                    visitedOrder[visitedIndx++] = {currR, currC};\n                }\n            }\n            upLimit--;\n        }\n        return visitedOrder;\n    }\n\nprivate:\n    bool isInGrid(int r, int c, int rows, int cols) {\n        return r > -1 && c > -1 && r < rows && c < cols;\n    }\n};\n\n```\n\n
['Array', 'Matrix', 'Simulation', 'C++', 'Java']