4/06/2014

Leetcode -- Rotate Image

Not in place
class Solution {  
 public:  
   void rotate(vector<vector<int> > &matrix) {  
     int n = matrix.size();  
     vector<vector<int> > newMatrix(n, vector<int>(n, 0));  
     for(int i = 0; i <n; ++i) {  
       for(int j = 0; j < n; ++j) {  
         newMatrix[j][n - i - 1] = matrix[i][j];  
       }  
     }  
     swap(matrix, newMatrix);  
   }  
 };  

No comments: