#include<iostream>
#include<vector>
using namespace std;
class Solution{
public:
void rotate(vector<vector<int>> &matrix){
if(matrix.empty()){
return ;
}
int layer_num = matrix.size()/2;
int column_num = matrix.size();
int row_num = matrix.size();
for(int i = 0; i<layer_num ; i++){
// 起始点注意
for(int j = i;j<matrix.size()-i-1;j++){
int tmp = matrix[i][j];
//明确变化的是行还是列,然后跟j对应(j是不断变化的)
matrix[i][j] = matrix[row_num-j-1][i];
matrix[row_num-j-1][i] = matrix[row_num -i -1][column_num-j-1];
matrix[row_num -i -1][column_num-j-1] = matrix[j][column_num-i-1];
matrix[j][column_num-i-1]= tmp;
}
}
}
};
int main(){
Solution so;
vector<vector<int>> nums ={{1,2,3},{4,5,6},{7,8,9}};
so.rotate(nums);
for(int i=0;i<nums.size();i++){
for(int j=0;j<nums[i].size();j++){
cout << "the res is: " << nums[i][j] << endl;
}
}
}
leetcode no48_rotateImage
最新推荐文章于 2024-04-28 11:28:18 发布