C++ concise solution.


  • 8
    C
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n, vector<int> (n, 1));
        int left, right, top, down, index;
        left = top = index = 0, right = down = n-1;
        while (left <= right && top <= down) {
            for (unsigned int j = left; j <= right; j++)
                res[top][j] = ++index;
            top++;
            for (unsigned int i = top; i <= down; i++)
                res[i][right] = ++index;
            right--;
            for (int j = right; j >= left; j--)
                res[down][j] = ++index;
            down--;
            for (int i = down; i >= top; i--)
                res[i][left] = ++index;
            left++;
        }
        return res;
    }

  • 0
    C

    I really like you method of managing top, down, left, right!! thanks for sharing.


Log in to reply
 

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