Rotate Image
Total Accepted: 36552 Total Submissions: 114426My Submissions
Question Solution
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
Hide Tags
Have you met this question in a re
这道题将矩阵先转置,再将每一行进行旋转就可以了
#include<iostream> #include<algorithm> #include<vector> using namespace std; void rotate(vector<vector<int>>& matrix) { int n=matrix.size(); for(int i=0;i<n;i++) for(int j=i;j<n;j++) {int temp;temp=matrix[j][i];matrix[j][i]=matrix[i][j];matrix[i][j]=temp;} for(int i=0;i<n;i++) reverse(matrix[i].begin(),matrix[i].end()); } int main() { }
时间: 2024-11-05 20:12:30