Leetcode 48 Rotate Image

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010821666/article/details/82749618

Leetcode 48 Rotate Image

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
      //先转置,再说,ij交换是转置
      if(matrix.empty())
	return;
      int row = matrix.size();
      int col = matrix[0].size();
      
      for(int i = 0;i < row;++i){
	for(int j = i;j < col;++j){
	  //j's begining is i
	  int temp = matrix[i][j];
	  matrix[i][j] = matrix[j][i];
	  matrix[j][i] = temp;//直接swap 更快
	}
	//break;
      }
      //转置之后行序不变,列序变成n-j-1
      for(int i = 0;i < row;++i){
	for(int j = 0;j < col/2;++j){
	  int temp = matrix[i][j];
	  matrix[i][j] = matrix[i][col-j-1];
	  matrix[i][col-j-1] = temp;//直接reverse每一行也是一样的,应该更快。
	}
      }  
   }
};

猜你喜欢

转载自blog.csdn.net/u010821666/article/details/82749618