Accepted cpp solution,5ms,O(1)space and O(n)time


  • 0
    H

    class Solution {
    public:
    void rotate(vector<vector<int>>& matrix) {
    int n = matrix.size();
    int start = 0;
    while (n>1){
    for (int i = 0; i<n - 1; ++i){
    int temp = matrix[start][start+i];
    matrix[start][start+i] = matrix[start+n - 1- i][start];
    matrix[start+n - 1 - i][start] = matrix[start + n - 1][start+n - 1 - i];
    matrix[start + n - 1][start+n - 1 - i] = matrix[start+i][start + n - 1];
    matrix[start+i][start + n - 1] = temp;
    }
    n = n - 2;
    start++;
    }
    }
    };


Log in to reply
 

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