Two Pointers
We can use two pointers to simulate gravity for each row (or column after the rotation).\n\nC++ (204 ms)\ncpp\nvector<vector<char>> rotateTheBox(vector<vector<c
We can use two pointers to simulate gravity for each row (or column after the rotation).\n\n**C++ (204 ms)**\n```cpp\nvector<vector<char>> rotateTheBox(vector<vector<char>>& box) {\n    int m = box.size(), n = box[0].size();\n    vector<vector<char>> res(n, vector<char>(m, \'.\'));\n    for (int i = 0; i < m; ++i)\n        for (int j = n - 1, k = n - 1; j >= 0; --j)\n            if (box[i][j] != \'.\') {\n                k = box[i][j] == \'*\' ? j : k;\n                res[k--][m - i - 1] = box[i][j];\n            }\n\n    return res;\n}\n```\n**Java (8 ms)**\n```java\npublic char[][] rotateTheBox(char[][] box) {\n    int m = box.length, n = box[0].length;\n    char [][] res = new char[n][m];\n    for (int i = 0; i < m; ++i)\n        for (int j = n - 1, k = n - 1; j >= 0; --j) {\n            res[j][m - i - 1] = \'.\';\n            if (box[i][j] != \'.\') {\n                k = box[i][j] == \'*\' ? j : k;\n                res[k--][m - i - 1] = box[i][j];\n            }\n        }\n    return res;\n}\n```
['C', 'Java']