6ms c++ solution


  • 0
    S

    \
    class Solution {
    public:
    void rotate(vector<vector<int>>& matrix) {
    int n = matrix.size();
    vector<vector<int>> matrixNew(n, vector<int>(n,0));

        for(int j = 0; j < n ; j++){
            for(int i = 0; i < n ; i++){
                matrixNew[i][j] = matrix[-j+(n-1)][i];
            }
        }
        
        matrix = matrixNew;
    }
    

    };
    \\


  • 0
    S

    use the formula:
    [x', y', 1] = [0 -1 0, 1 0 0, 0 0 1][x, y, 1]
    x, y is the original coordinate.
    rotate transformation should be this,
    [cos90 -sin90 0, sin90 0 0, 0 0 1].
    After all, need shift the x index to positive position.


Log in to reply
 

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