[InterviewBit] Rotate Matrix

Rotate Matrix

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void helper(vector<vector<int>>& A, int l, int r) {
if(l >= r) return;
vector<int> row;
for(int i = l; i <= r; i++) row.push_back(A[l][i]);

for(int i = l, j = r; i <= r; i++,j--) A[l][j] = A[i][l];
for(int i = l; i <= r; i++) A[i][l] = A[r][i];
for(int i = l, j = r; i <= r; i++,j--) A[r][i] = A[j][r];
for(int i = l; i <= r; i++) A[i][r] = row[i-l];

helper(A,l+1,r-1);
}
void Solution::rotate(vector<vector<int> > &A) {
helper(A,0,A.size() - 1);
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/16/PS/interviewbit/rotate-matrix/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.