Thursday, June 23, 2016

48. Rotate Image

Image clockwise 90 degree rotation can be done by two steps:
1. swap elements along right diagonal
2. swap elements in vertical symmetry.

1:  class Solution {  
2:  public:  
3:    void rotate(vector<vector<int>>& matrix) {  
4:      for (int i = 0; i < matrix.size(); i++) {  
5:        for (int j = i+1; j < matrix[0].size(); j++) {  
6:          swap(matrix[i][j], matrix[j][i]);  
7:        }  
8:      }  
9:      for (int i = 0; i < matrix.size(); i++) {  
10:        for (int j = 0; j < matrix[0].size()/2; j++) {  
11:          swap(matrix[i][j], matrix[i][matrix[0].size()-j-1]);  
12:        }  
13:      }  
14:    }  
15:  };  

No comments:

Post a Comment