knakul853

Untitled

Jun 11th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. /**
  2. knakul853
  3. https://leetcode.com/problems/rotate-image/discuss/18872/A-common-method-to-rotate-the-image
  4. **/
  5. class Solution {
  6. public:
  7.     void rotate(vector<vector<int>>& matrix) {
  8.        
  9.         int n = (int)matrix.size();
  10.      
  11.          for(int  i=0;i<(n)/2; i++ )
  12.          {
  13.              for(int j=0;j<(n+1)/2;j++ )
  14.              {
  15.                  int pre = matrix[i][j];
  16.                  
  17.                  for(int k=0;k<4;k++)
  18.                  {
  19.                      int new_i = j;
  20.                      int new_j = n-1-i;
  21.                      
  22.                      int temp = matrix[new_i][new_j];
  23.                      matrix[new_i][new_j] = pre;
  24.                      pre = temp;
  25.                      
  26.                      i = new_i;
  27.                      j = new_j;
  28.                  }
  29.              }
  30.          }
  31.        
  32.     }
  33. };
Add Comment
Please, Sign In to add comment