First row, rotate, repeat (C++)

  • 1

    Not as cute as in Python or in Ruby, but still ok. Accepted in 0 ms.

    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> out;
        while (matrix.size()) {
            out.insert(out.end(), matrix[0].begin(), matrix[0].end());
            auto copy = matrix;
            matrix = vector<vector<int>>(copy[0].size(), vector<int>(copy.size() - 1));
            for (int i=0; i<matrix.size(); i++)
                for (int j=1; j<copy.size(); j++)
                    matrix[i][j-1] = copy[j][matrix.size()-1-i];
        return out;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.